php将日期转换为时间戳 [英] php convert a date to a timestamp
问题描述
我正在尝试将2个日期转换为时间戳。一个由php(今天的日期)生成,另一个由用户输入
I'm trying to convert 2 dates to timestamps. One is generated by php (today's date) and the other is input by the user
$today = date("d-m-y"); // 01-12-13
$start_date = "28-11-13";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
问题是结果不正确
结果:
01-12-13> 28-11-13 1008201600> 1857686400
01-12-13 > 28-11-13 1008201600 > 1857686400
有什么问题?
推荐答案
始终使用四位数的年份,数字年代导致无尽头痛。 13
不被视为一年;相反, 01-12-13
和 28-11-13
被解释为2001年12月13日和11月13日, 2028由PHP。请参阅ISO8601符号下的 PHP的日期格式页面:两位数的年份,月份和日期与破折号。
Always use four-digit years, as using two-digit years leads to endless headaches. 13
is not treated as a year; instead, 01-12-13
and 28-11-13
are interpreted as December 13, 2001 and November 13, 2028 by PHP. See PHP's Date Formats page, under "ISO8601 Notations": "Two digit year, month and day with dashes".
如果您使用 2013
,则问题消失: / p>
If you use 2013
instead, the issues disappear:
$today = date("d-m-Y"); // 01-12-2013
$start_date = "28-11-2013";
$todaytimestamp = strtotime($today);
$starttimestamp = strtotime($start_date);
echo "$today > $start_date <br>$todaytimestamp > $starttimestamp";
输出:
01-12-2013 > 28-11-2013
1385884800 > 1385625600
这篇关于php将日期转换为时间戳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!