当前日期无法在PHP中格式化
I'm using the jquery datepicker, and when today's date is selected, it's failing to convert to the format I need for a database query.
The datepicker date format is 'm-d-Y', so today appears as 09-16-2013. This part is working, and the datepicker correctly displays today's date as the default selection.
However, I need to use 'Y-m-d' format to query the database. This works fine when the date is less than today. But for today, it's failing and I'm getting 1969-12-31.
echo $enddt;
displays: 09-16-2013
I have tried:
echo date('Y-m-d', strtodate($enddt));
echo date('Y-m-d H:i:s', strtodate($enddt));
and neither works. How do I format $enddt so it displays 2013-09-16 ?
我正在使用jquery datepicker,当选择今天的日期时,它无法转换为我需要的格式 对于数据库查询。 p>
datepicker日期格式为'md-Y',所以今天显示为09-16-2013。 这部分正常工作,而datepicker正确显示今天的日期 作为默认选择。 p>
但是,我需要使用'Ymd'格式来查询数据库。 当日期小于今天时,此工作正常。 但是今天,它失败了,我得到了1969-12-31。 p>
echo $ enddt;
code> pre>
显示:09-16-2013 p>
我尝试过: p>
echo date('Ym-d',strtodate($ enddt));
echo date('Ymd H:i:s',strtodate($ enddt));
code> pre>
并且都不起作用。
我如何格式化 $ enddt所以显示2013-09-16? p>
div>
The function is actually strtotime()
, not strtodate()
:
Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Since you have the date string separated by dashes, we'll need to convert it into slashes first to make strtotime()
recognize the format as mm/dd/yyyy
:
$enddt = '09-16-2013';
$enddt = str_replace('-', '/', $enddt);
echo date('Y-m-d', strtotime($enddt));
$enddt = DateTime::createFromFormat('m-d-Y', '09-16-2013');
echo $enddt->format('Y-m-d');