这就是我想要达到的目标:
1)用户从表单提交roster_input(这里我只是设置了一个变量作为示例输入)
2)php将其更改为时间戳并减去7500秒
3)php显示新的时间,但在不同的时区
显然我做错了什么…但这是我第一次和约会打交道!
$roster_input='14:15' ;
$timestamp = strtotime($roster_input) - 7500;
$date = new DateTime($timestamp, new DateTimeZone('Pacific/Nauru'));
echo $date;
也试了,没有成功:
$date = DateTime::createFromFormat('H:i', $timestamp, new DateTimeZone('Pacific/Nauru'));
根据PHP手册
$date = new DateTime(null, new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "'n";
因此,您必须使用有效的日期字符串:
这个作品。返回12:10
$roster_input='14:15' ;
$timestamp = strtotime($roster_input) - 7500;
$format = date("H:i", $timestamp );
$date = new DateTime($format, new DateTimeZone('Pacific/Nauru'));
echo $date->format('H:i') . "'n"; //Note how you echo the result
THIS failed(直接使用时间戳);
$roster_input='14:15' ;
$timestamp = strtotime($roster_input) - 7500;
$date = new DateTime($timestamp , new DateTimeZone('Pacific/Nauru'));
echo $date->format('H:i') . "'n";
致命错误:未捕获的异常' exception '与消息'DateTime::_construct() [DateTime . DateTime . net]。——construct]:在位置8(0)处解析时间字符串(1362417000)失败:在writecodeonline.com/php:7 Stack trace: #0 writecodeonline.com/php(7):中出现意外字符' DateTime->_construct('1362417000', Object(DateTimeZone)) #1 {main}抛出在第7行
直接使用时间戳:返回17:10
$roster_input='14:15' ;
$timestamp = strtotime($roster_input) - 7500;
$date = new DateTime('@'.$timestamp , new DateTimeZone('Pacific/Nauru')); //Notice '@'.
echo $date->format('H:i') . "'n";
适合您的目标结果。只是要注意不同的结果阅读更多:http://www.php.net/manual/en/datetime.construct.php
你似乎走对了路。唯一的问题是,您不能直接将对象转储为字符串。
试着把最后一行改成这样:
echo $date->format("c");
或者将原文改成3个字符:
$roster_input='14:15';
$timestamp = strtotime($roster_input) - 7500;
$date = new DateTime("@$timestamp", new DateTimeZone('Pacific/Nauru'));
echo $date->format(DATE_RFC822);