当前位置: 代码迷 >> 综合 >> php strtotime 时间函数错误使用
  详细解决方案

php strtotime 时间函数错误使用

热度:96   发布时间:2023-11-26 21:12:36.0

php 时间函数  strtotime  :将任何字符串的日期时间描述解析为Unix时间戳;

注意:日期格式   ===》  时间戳; 如果是不按照规定输入 时间戳呢?会不会还返回时间戳呢?

答案是肯定:会个大毛线啊!!!种下西瓜能长出人参嘛。

错误背景:数据表的设计,最初的设计时间的存储采用 int型,存入时间戳,认为这样可以节省资源。然而随着时间的推移,时光荏苒,花开花落,再设计表时,认为存入TEMESTAMP,更好。因为资源在现在市场环境下,相对来说没有那么贵了。所以为了更好的分析数据,更直观,所以改变了时间的存储方式。

表结构的改变,项目中公共方法也与时俱进,采用strtotime转换,然后比较时间的大小。

//验证时间戳 二次(strtotime)转换 $time_int = 1523761272;       //2018-04-15 11:01:12 $time_date = '2018-04-15 11:01:12';


if(strtotime($time_date) < time()){
    echo 1;
}else{
    echo 0;
}
当前时间2018-03-15 11:47:00   所以返回0;

if(strtotime($time_int) < time()){
    echo 1;
}else{
    echo 0;
}
当前时间2018-03-15 11:48:00   所以返回1;

打印:

$change_time = strtotime($time_int); false

显然,传入错误,肯定返回错误。不可能 奇*偶 = 偶。

其实,写这篇博文没有任何意义,究其原因,就是因为对php的函数的基本用户不了解,导致报错。

对症下药:掌握php函数的基本用法,避免逻辑错误。

赠品:

        echo strtotime("+1 day"), PHP_EOL;

echo strtotime("+1 week"), PHP_EOL;

echo strtotime("+1 week 2 days 4 hours 2 seconds"), PHP_EOL;

echo strtotime("next Thursday"), PHP_EOL;

echo strtotime("last Monday"), PHP_EOL;


我为人人,人人为我;美美与共,天下大同。