我的代码
$time = "Tuesday, 26 June 2012";
//str_replace(',','',$time);<--this also doesn't work.
$a = strptime($time, "%l, %j %F %Y");
$stmp = mktime(0,0,0,$a['tm_mon'],$a['tm_mday'],$a['tm_year'],0);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp; //Tuesday, 30 November 1999 00:00:00
现在我知道有一种更优雅的方式,实际上有效:
$time = "Tuesday, 26 June 2012";
$stmp = strtotime($time);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp;//Tuesday, 26 June 2012 00:00:00
但是第一个版本出了什么问题?我只是好奇。
答案 0 :(得分:4)
$a = strptime(time, "%l, %j %F %Y");
你写了time
;它应该是$time
。
您的格式字符串错误。 strptime
不使用与date
相同的格式字符串,只是前面有百分号;它有自己的一套。您的格式字符串应如下所示:
$a = strptime($time, "%A, %e %B %Y");
strptime
自1900年以来返回年。。您需要添加1900
。strptime
返回0到11之间的月份。您需要添加1
。这是您的代码,已修复:
<?php
$time = "Tuesday, 26 June 2012";
$a = strptime($time, "%A, %e %B %Y");
$stmp = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], 1900 + $a['tm_year'], 0);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp;
?>
答案 1 :(得分:2)
你仍然必须删除它应该正确的mktime函数的最后0,
$time = "Tuesday, 26 June 2012";
$a = strptime($time, "%A, %e %B %Y");
$stmp = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], 1900 + $a['tm_year']);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp;
答案 2 :(得分:1)
$ time格式错误,应为“%A,%e%B%Y”。
如果你是var_dump $ a,
array(9) {
["tm_sec"]=>
int(0)
["tm_min"]=>
int(0)
["tm_hour"]=>
int(0)
["tm_mday"]=>
int(26)
["tm_mon"]=>
int(5)
["tm_year"]=>
int(112)
["tm_wday"]=>
int(2)
["tm_yday"]=>
int(177)
["unparsed"]=>
string(0) ""
}
如果您看到文档,则会找到返回的实际值。
"tm_sec" Seconds after the minute (0-61)
"tm_min" Minutes after the hour (0-59)
"tm_hour" Hour since midnight (0-23)
"tm_mday" Day of the month (1-31)
"tm_mon" Months since January (0-11)// increment month
"tm_year" Years since 1900// add years from 1900
"tm_wday" Days since Sunday (0-6)
"tm_yday" Days since January 1 (0-365)
"unparsed" the date part which was not recognized using the specified format
你找到传递给你的mktime的实际值,调整传入mktime的val来解决这个问题。
<?
$time = "Tuesday, 26 June 2012";
$a = strptime($time, "%A, %e %B %Y");
var_dump($a);
$stmp = mktime(0, 0, 0, $a['tm_mon'] + 1, $a['tm_mday'], 1900 + $a['tm_year']);
$from_stmp = date("l, j F Y H:i:s", $stmp);
echo $from_stmp;
?>
答案 3 :(得分:0)