0

hello i am working with the Date() function i am getting a time that is current and time that is coming from a database to compare the times, but the dates are different:

date_default_timezone_set("America/Los_Angeles"); // set time zone to LA
$date = date("m-d-Y h:i:s"); // current time
$current_time = strtotime($date); // current time in seconds
$get_time = 1357487529; //linux time from the server
$difference = $current_time - $get_time; //seconds that have pass already
$get_date = date("m-d-Y h:i:s", $get_time); // convert the linux time to current time and date
$exploded_get_date = explode(" ", $get_date); //divide the get date into 2 parts by space 0 = date 1 = time
$exploded_current_date = explode(" ", $date); //divide the current date into 2 parts by space 0 = date 1 = time

the results i get are:

01-Sun-2013 07:52:09 //get date
06-01-2013 07:56:25 //current date
1357487785 // current time
1357487529 // get time
256 //difference 

why is it saying i have month 1 in the get date, but in the current date is actually month 6 and also the day it says it is Sunday 6, when is Saturday 1? how can i fix this?

4

2 回答 2

0

m-d-Y不是有效的解析格式。只有你们美国人认为将元素按未排序的顺序排列是明智的......

无论如何,关键是,06-01-2013 是什么意思?是 6 月 1 日还是 1 月 6 日?

为保持一致性,计算机假定为 1 月 6 日(dmY 格式)。

我强烈建议使用这种Y-m-d H:i:s格式,因为它是完全大端序的,本质上可以作为字符串排序。

编辑:应该注意,您可以只使用time()来获取当前时间戳。

于 2013-06-02T03:10:40.950 回答
0

您的代码非常多余:

$date = date("m-d-Y h:i:s"); // current time
$current_time = strtotime($date); // current time in seconds

可以用一个简单的替换

$current_time = time();

$get_date = date("m-d-Y h:i:s", $get_time); // convert the linux time to current time and date
$exploded_get_date = explode(" ", $get_date); //divide the get date into 2 parts by space 0 = date 1 = time
$exploded_current_date = explode(" ", $date);

可能只是

$exploded_date = date('m-d-y', $get_time);
$exploded_time = date('h:i:s', $get_time);

您在无用/重复和最终冗余的操作上浪费了大量的 CPU 周期。

从更大的角度来看,您的错误是 PHP 的正常和最容易分析/解析的日期/时间字符串是yyyy-mm-dd格式的。你正在建造mm-dd-yyyy,这几乎是完全打乱的。当您输入不确定的格式时,PHP 无法正确猜测。这意味着 strtotime() 会搞砸并给你不正确的结果。

于 2013-06-02T03:11:20.180 回答