我试图获得一个完整的日期,给定一个字符串,这样 03-30-1986 将导致 1986 年 3 月 30 日。
我已经尝试了以下代码。
$date = 03.30.1986
$mydate = strtoTime($date);
$printdate = date('m-d-Y', $mydate);
我使用 echo 查看 $printdate 的结果,但结果是它的值为空。想法?
$date = '03.30.1986';
$mydate = strtoTime($date);
echo $printdate = date('F d, Y', $mydate);
这是工作只需添加报价...
Result :March 31, 1969
$date = '03.30.1986';
$temp = explode('.',$date);
$date = date("m.d.Y", mktime(0, 0, 0, $temp[0], $temp[1],$temp[2]));
echo $date;
我的想法:
strtotime()
需要一个字符串:($date = "03.30.1986"
注意str
函数名的部分);
:$date = "03.30.1986";
"F d, Y"
所以你的代码变成:
$date = "03.30.1986";
$mydate = strtoTime($date);
$printdate = date('F d, Y', $mydate);
您的第一行有几个错误(缺少引号、分号),strtotime 无法解析该日期格式,并且您为所需的输出使用了错误的格式。这应该有效:
$date = '03.30.1986';
$parts = explode('.', $date);
$mydate = mktime(0, 0, 0, $parts[0], $parts[1], $parts[2]);
$printdate = date('F d, Y', $mydate);
php 假定它为欧洲日期格式。更多请看这里
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.