PHP:使用DateTime类转换日期 [英] PHP: convert date with DateTime Class

查看:671
本文介绍了PHP:使用DateTime类转换日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个日期是从PayPal IPN

  payment_date = 20:12:59 2009年1月13日PST 

如何在中转换Ymd\TH:i:s\Z



我尝试用

  $ date = new DateTime($ _ POST ['payment_date']); 
$ payment_date = $ date->格式('Y-m-d\TH:i:s\Z');

但在我的数据库中,我只获取'0000-00-00 00: 00:00'



谢谢

解决方案

DateTime应该可以解析这个格式:

  $ str = '20:12:59 2009年1月13日PST '; 
$ date = new DateTime($ str);
$ date-> setTimezone(new DateTimezone('UTC'));
$ payment_date = $ date->格式('Y-m-d\TH:i:s\Z');
echo $ payment_date;

输出

  2009-01-13T20:12:59Z 

这应该按预期工作。您的 $ _ POST ['payment_date'] 变量可能包含一些额外的字符在开头或结尾。您可以在 $ _ POST 变量上尝试 trim(),以确保开头没有任何空格或结束。


I've this date from PayPal IPN

payment_date = 20:12:59 Jan 13, 2009 PST

How i can convert in Y-m-d\TH:i:s\Z ?

I did try with

$date = new DateTime($_POST['payment_date']);
$payment_date = $date->format('Y-m-d\TH:i:s\Z');

But in my DB I obtain only '0000-00-00 00:00:00'

Thank you

解决方案

DateTime should be able to parse this format:

$str = '20:12:59 Jan 13, 2009 PST';    
$date = new DateTime($str);
$date->setTimezone(new DateTimezone('UTC'));
$payment_date = $date->format('Y-m-d\TH:i:s\Z');
echo $payment_date;

Output:

2009-01-13T20:12:59Z

This should work as expected. Your $_POST['payment_date'] variable may contain some extra characters at the beginning or end. You can try trim() on the $_POST variable to make sure there aren't any whitespace in the beginning or end.

这篇关于PHP:使用DateTime类转换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆