PHP - 将日期转换为 UTC 日期
我从 mysql 数据库中提取了一个格式为 YYYY-MM-DD 的日期。
现在我有一些 javascript 需要这样的日期:
data: [
[Date.UTC(2010, 0, 1), 29.9],
[Date.UTC(2010, 2, 1), 71.5],
[Date.UTC(2010, 3, 1), 106.4]
]
所以我想我可以执行以下操作:
while($row = mysql_fetch_array($sql)){
$thedate = explode('-', $row['date']); //$row['date'] = YYYY-MM-DD
$str .= '[Date.UTC(' . $thedate[0].','.$thedate[1].','.$thedate[2].'), '.$row['answer'].'],';
$i++;
}
然后使用变量 data
中的 AJAX 请求在浏览器端简单地捕获此内容,给出:
data: [data]
发出警报时我注意到 22 日和 22 日的数据内容如下: 8 月 23 日:
[Date.UTC(2011, 08, 22), 55], [Date.UTC(2011, 08, 23), 65]
当我需要的是
[Date.UTC(2011, 7, 22), 55], [Date.UTC(2011, 7, 22), 65]
有人可以告诉我如何将从数据库关系中提取的日期转换为正确的格式....
YYYY
I have a date pulled from my mysql database in the form YYYY-MM-DD.
Now I have some javascript that requires the dates like this:
data: [
[Date.UTC(2010, 0, 1), 29.9],
[Date.UTC(2010, 2, 1), 71.5],
[Date.UTC(2010, 3, 1), 106.4]
]
So I thought I could do the following:
while($row = mysql_fetch_array($sql)){
$thedate = explode('-', $row['date']); //$row['date'] = YYYY-MM-DD
$str .= '[Date.UTC(' . $thedate[0].','.$thedate[1].','.$thedate[2].'), '.$row['answer'].'],';
$i++;
}
And then simply capture this in the browser side using an AJAX request in a variable data
, giving:
data: [data]
When alerting the contents of data I notice I get something like the following for 22nd & 23rd August:
[Date.UTC(2011, 08, 22), 55], [Date.UTC(2011, 08, 23), 65]
When what I need is
[Date.UTC(2011, 7, 22), 55], [Date.UTC(2011, 7, 22), 65]
Can someone tell me how to convert convert the date pulled from the db relation into the correct format....
YYYY
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
那是什么格式?无论如何,使用
DateTime::createFromFormat
解析日期,之后您可以按照您喜欢的方式格式化它。示例:
顺便看一下
json_encode
http ://php.net/manual/en/function.json-encode.phpWhich format is that? Anyway, use
DateTime::createFromFormat
to parse the date, after that you can format it anyway you like.Example:
By the way, have a look at
json_encode
http://php.net/manual/en/function.json-encode.php关于 DateTime - 这是一个很棒的类,但仅适用于 pphp >= 5.2
about DateTime - this is a great class but avaliable only for pphp >= 5.2