PHP 中的 strtotime for dd MM yyyy
strtotime in PHP for dd MM yyyy
尝试时:
echo "<br />".$opening_time = "02 May 2019 - 03:10";
echo "<br />".$closing_time = "12 May 2019 - 13:40";
echo "<br />".$string_opening_time = strtotime($opening_time);
echo "<br />".$string_closing_time = strtotime($closing_time);
echo "<br />".$diffrence_time = $string_closing_time - $string_opening_time;
结果是:
02 May 2019 - 03:10 12
May 2019 - 13:40
//2 blank lines
0
为什么我转成strtotime时是空白的?
无法识别格式 d M Y - H:i
,但如果您知道使用 DateTime::createFromFormat()
.
的格式,您可以将其重新创建为 DateTime 对象
创建两个 DateTime 对象并对它们使用 diff()
方法,这会给您带来不同。
$opening_time = "02 May 2019 - 03:10";
$closing_time = "12 May 2019 - 13:40";
$open = DateTime::createFromFormat("d M Y - H:i", $opening_time);
$close = DateTime::createFromFormat("d M Y - H:i", $closing_time );
$diff = $open->diff($close);
echo $opening_time."<br />\n";
echo $closing_time."<br />\n";
echo $diff->d." days ".$diff->h." hours ".$diff->m." minutes ";
如果您需要以秒为单位的差异,请使用 getTimestamp()
方法。
$open = DateTime::createFromFormat("d M Y - H:i", $opening_time);
$close = DateTime::createFromFormat("d M Y - H:i", $closing_time );
$diff = $close->getTimestamp() - $open->getTimestamp();
echo $opening_time."<br />\n";
echo $closing_time."<br />\n";
echo $diff;
尝试时:
echo "<br />".$opening_time = "02 May 2019 - 03:10";
echo "<br />".$closing_time = "12 May 2019 - 13:40";
echo "<br />".$string_opening_time = strtotime($opening_time);
echo "<br />".$string_closing_time = strtotime($closing_time);
echo "<br />".$diffrence_time = $string_closing_time - $string_opening_time;
结果是:
02 May 2019 - 03:10 12
May 2019 - 13:40
//2 blank lines
0
为什么我转成strtotime时是空白的?
无法识别格式 d M Y - H:i
,但如果您知道使用 DateTime::createFromFormat()
.
创建两个 DateTime 对象并对它们使用 diff()
方法,这会给您带来不同。
$opening_time = "02 May 2019 - 03:10";
$closing_time = "12 May 2019 - 13:40";
$open = DateTime::createFromFormat("d M Y - H:i", $opening_time);
$close = DateTime::createFromFormat("d M Y - H:i", $closing_time );
$diff = $open->diff($close);
echo $opening_time."<br />\n";
echo $closing_time."<br />\n";
echo $diff->d." days ".$diff->h." hours ".$diff->m." minutes ";
如果您需要以秒为单位的差异,请使用 getTimestamp()
方法。
$open = DateTime::createFromFormat("d M Y - H:i", $opening_time);
$close = DateTime::createFromFormat("d M Y - H:i", $closing_time );
$diff = $close->getTimestamp() - $open->getTimestamp();
echo $opening_time."<br />\n";
echo $closing_time."<br />\n";
echo $diff;