【问题标题】:php time till specific date and timephp 时间到特定日期和时间
【发布时间】:2014-11-12 18:03:39
【问题描述】:

这是我当前的代码:

$nm = strtotime('next monday 19:30');
$nt = strtotime('next wednesday 19:30');
$ns = strtotime('next Saturday 09:00');

有点用,但假设是星期三,它不会告诉我 X 小时之类的信息,它会告诉我 1 周 X 小时。

我该如何解决这个问题?

您可以在此处查看现场演示:http://www.forumalliance.x10.mx/troll.php

【问题讨论】:

  • 看看date_diff

标签: php time strtotime


【解决方案1】:

要计算weekshours首先得到nowdesired time之间的时间间隔。然后将interval 除以相当于 7 天的秒数,得到周数,mod 得到休息时间。示例:

$nm = strtotime('next monday 19:30');
$interval = $nm - time();
$week = floor($interval / (7 * 24 * 60 * 60));
$hour = floor(($interval % (7 * 24 * 60 * 60)) / 3600);

echo $week . ' week ' . $hour . ' hour(s)';

【讨论】:

  • 这给了我 0 周和 120 小时 :S
  • 因为 1 周是 168 小时,但间隔是 120 小时。所以它显示 0 周。
【解决方案2】:

这得到了我需要的东西。

$day = date('l');
$hour = date('H');
$min = date('i');


if ($hour <= 19 and $min < 30 and $day == "Monday") {
    $nm = strtotime('this monday 19:30');
}else{
    $nm = strtotime('next monday 19:30');
}

if ($hour <= 19 and $min < 30 and $day == "Wednesday") {
    $nt = strtotime('this wednesday 19:30');
}else{
    $nt = strtotime('next wednesday 19:30');
}

if ($hour <= 09 and $min < 00 and $day == "Saturday") {
    $ns = strtotime('this saturday 09:00');
}else{
    $ns = strtotime('next saturday 09:00');
}

【讨论】:

    猜你喜欢
    • 2013-06-16
    • 2014-04-20
    • 2010-10-13
    • 2012-05-27
    • 2013-10-28
    • 2010-11-05
    • 1970-01-01
    • 2018-11-15
    • 1970-01-01
    相关资源
    最近更新 更多