且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP:使用DateTime类转换日期

更新时间:2023-11-29 12:15:46

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.