php 向时间戳添加 13 小时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1124752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Add 13 hours to a timestamp
提问by aalaap
I have a table with timestamp values like:
我有一个带有时间戳值的表,例如:
2009-07-14 02:00:00
I need to display them at run time with 13 hours added, like:
我需要在运行时显示它们并添加 13 小时,例如:
2009-07-14 15:00:00
What's the simplest way to do this in PHP?
在 PHP 中执行此操作的最简单方法是什么?
采纳答案by aalaap
I know this was asked a long time ago and I had already chosen the answer, but I believed it's important to point new users in a more modern and correct direction, which is using the fantastic Carbonlibrary:
我知道很久以前就有人问过这个问题,我已经选择了答案,但我相信将新用户指向更现代和正确的方向很重要,即使用梦幻般的Carbon库:
>>> Carbon\Carbon::parse('2009-07-14 02:00:00')
... ->addHours(13)
... ->format('Y-m-d H:i:s');
=> Carbon\Carbon {#869
+"date": "2009-07-14 15:00:00.000000",
+"timezone_type": 3,
+"timezone": "UTC",
}
Carbon not only makes it simpler to perform the requested calculation, it also shields you against any potential mistakes you may make while manually trying to do the same thing.
Carbon 不仅使执行请求的计算变得更简单,而且还可以防止您在手动尝试执行相同操作时可能犯的任何潜在错误。
The shell in the above output example is Laravel Tinker. Laravel ships with Carbon, so the above statement works out-of-the-box. If you're not using a modern PHP framework, you should be. It's easy to add Carbon to it.
上面输出示例中的 shell 是 Laravel Tinker。Laravel 附带了 Carbon,所以上面的语句是开箱即用的。如果您没有使用现代 PHP 框架,那么您应该使用。向其中添加碳很容易。
回答by macbirdie
I know that
我知道
date( "Y-M-d H:i:s", strtotime( $timestamp_from_array ) + 13 * 3600 );
is smelly, but it will give you an idea.
很臭,但它会给你一个想法。
strtotimeconverts the timestamp string to a timestamp value, then we add the hours and convert it back to the timestamp format in the array with the datefunction.
strtotime将时间戳字符串转换为时间戳值,然后我们添加小时数并使用date函数将其转换回数组中的时间戳格式。
But I suppose what you really want is to use time zones.
但我想你真正想要的是使用时区。
Edit: igstanis correct, you should also mind the daylight saving time changes between those offsets.
编辑: igstan是正确的,您还应该注意这些偏移量之间的夏令时变化。
回答by Ionu? G. Stan
There are lots of problems with Time Zones and Daylight Saving Time when using basic arithmetic for time calculations. The best solution is to rely on PHP's own date/time functions, like strtotime()or mktime(). I wrote about DST problems on my blog.
在使用基本算法进行时间计算时,时区和夏令时存在很多问题。最好的解决方案是依赖 PHP 自己的日期/时间函数,如strtotime()或mktime()。我在我的博客上写了关于DST 问题的文章。
echo date('Y-M-d H:i:s', strtotime('2009-07-14 02:00:00 + 13 hours'));
回答by n8wrl
Can you do the add as it comes out of the database?
你能在数据库中添加吗?
SELECT ... DateAdd(hh, 13, DateField) AS DateField
(SQL Server)
(SQL 服务器)
回答by Flo
you could use http://de3.php.net/manual/en/function.strptime.phpto convert it to a unix timestamp. Then you could easily add 13*60*60 to that and use http://de3.php.net/manual/en/function.date.phpto convert it back to a timestamp as you like.
您可以使用http://de3.php.net/manual/en/function.strptime.php将其转换为 unix 时间戳。然后您可以轻松地将 13*60*60 添加到其中并使用http://de3.php.net/manual/en/function.date.php将其转换回您喜欢的时间戳。
another way would be via the explode function, but i think this might be more complicated because you have to look if days/month/years change and stuff
另一种方法是通过爆炸功能,但我认为这可能更复杂,因为您必须查看天/月/年是否发生变化等等
回答by Richy B.
You have a table with the timestamps? If it's a MySQL database, you could just do this in the database using addtime: SELECT ADDTIME('2007-12-31 23:59:59.999999', '1 1:1:1.000002');
你有一个带有时间戳的表吗?如果它是 MySQL 数据库,您可以使用addtime在数据库中执行此操作:SELECT ADDTIME('2007-12-31 23:59:59.999999', '1 1:1:1.000002');
If you are using the PHP Zend Framework, you could do something like:
如果您使用的是 PHP Zend 框架,您可以执行以下操作:
$date=new Zend_Date(Array('year'=>$iYear,'month'=>$iMonth,'day'=>$iDay));
// changes $date by adding 12 hours
$date->add('12:00:00', Zend_Date::TIMES);
Alternatively, you could do it using native PHP4 functions such as: [faster, less accurate: doesn't account for leap seconds/time zone changes etc]
或者,您可以使用本机 PHP4 函数来实现,例如:[更快、更不准确:不考虑闰秒/时区更改等]
$datestamp=strtotime('2009-07-14 02:00:00'); // It'll probably be better using mktime
$datestamp=$datestamp+(60*60*13); // 60 seconds times 60 minutes times 13 hours
or [slower, more accurate]
或 [更慢,更准确]
$datestamp = strtotime('2009-07-14 02:00:00'); // It'll probably be better using mktime
$newDate = date('Y-m-d H:i:S', mktime(date('H',$datestamp)+13, date('i',$datestamp), date('s',$datestamp), date('m',$datestamp), date('d',$datestamp), date('Y',$datestamp)));
or, if you are using PHP 5 using the Datetimefunctions
或者,如果您使用 PHP 5 使用Datetime函数
$date = new DateTime('2009-07-14 02:00:00');
date_add($date, new DateInterval("P13H"));
回答by David Barnes
<?php
echo date('Y-m-d H:i:s', strtotime('+13 hours', strtotime('2009-07-14 02:00:00')));
?>
回答by tfont
Let's say you were picking a specific date field from your table called "datetime_field" from row ID 3000. It would be as simple as this!
假设您从行 ID 3000 中选择名为“ datetime_field”的表中的特定日期字段。就这么简单!
SELECT *, ADDTIME(`datetime_field`,'0 13:00:00') as new_datetime
FROM `table_name`
WHERE id = 3000
This can obviously be used in update queries too if one wanted to update, of course.
当然,如果想要更新,这显然也可以用于更新查询。
And alternatively, if you were just adding 13 hours (timezone differences) to the query. One would use the MySQL NOW()as the original time and simply add the time like above. Selecting, updating, or inserting,it's all the same methodology! It would be:
或者,如果您只是向查询添加 13 小时(时区差异)。可以使用 MySQL NOW()作为原始时间,然后像上面一样简单地添加时间。选择、更新或插入,都是一样的方法!这将是:
SELECT *, NOW() as current_datetime,
ADDTIME(NOW(),'0 13:00:00') as new_datetime
FROM `table_name`
WHERE id = 3000
Keep it simple and fast !
保持简单和快速!
:o)
:o)

