我正在尝试获取给定字符串的完整日期,例如03-30-1986将结果为1986年3月30日。
我试过下面的代码。
$date = 03.30.1986
$mydate = strtoTime($date);
$printdate = date('m-d-Y', $mydate);
我使用echo来查看$printdate的结果,但结果是它的值为null。想法吗?
$date = '03.30.1986';
$mydate = strtoTime($date);
echo $printdate = date('F d, Y', $mydate);
这是工作,只是添加引号…
Result :March 31, 1969
您的第一行有几个错误(缺少引号,分号),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);
我的想法
-
strtotime()
需要字符串:$date = "03.30.1986"
(注意函数名的str
部分) - PHP表达式用
;
:$date = "03.30.1986";
分隔 - 您需要将日期格式重新格式化为:
"F d, Y"
所以你的代码变成了:
$date = "03.30.1986";
$mydate = strtoTime($date);
$printdate = date('F d, Y', $mydate);
$date = '03.30.1986';
$temp = explode('.',$date);
$date = date("m.d.Y", mktime(0, 0, 0, $temp[0], $temp[1],$temp[2]));
echo $date;
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.