This question already has an answer here:
php strtotime() messes with date of a different year

(1个答案)


6年前关闭。




我正在尝试使用pickadate.js混合使用Laravel 4和PHP来转换日期。我遇到的问题是我尝试保存> 2013年的日期。如果选择2014年1月23日,它将保存为2013年1月23日。

这就是通过$_POST发送到date变量的内容。

23 January, 2014

这是我的设置
$date = strtotime($scheduler['date']);

转换为UNIX 1358993640(读取为2013年1月23日)
$dateFormat = date('l: F d, Y',$date);

变成:

Wednesday: January 23, 2013

我还有其他功能可以使用吗?还是我需要在strtotime之前以其他方式转换时间?它可以在2013年使用。因此,我想一旦它在2014年上市就可以了。

最佳答案

很奇怪。但是找到了解决办法。在运行PHP 5.5的本地设置上进行测试,似乎逗号是引起此问题的原因。因此,从您输入的数据中去除逗号会产生您的测试代码所需的结果:

// Set the test data.
$test_data = '23 January, 2014';

// Filter out commas from the '$test_data'
$test_data = preg_replace('/,/', '', $test_data);

// Get the Unix datetime from the test data.
$date = strtotime($test_data);

// Format the Unix datetime.
$dateFormat = date('l: F d, Y', $date);

// Output for debugging.
echo 'date: ' . $date . '<br />';
echo 'dateFormat: ' . $dateFormat . '<br />';

我得到的输出是:
date: 1390453200
dateFormat: Thursday: January 23, 2014

关于php - strtotime无法转换正确的年份,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20832513/

10-11 22:36
查看更多