向时间戳添加 13 小时 [英] Add 13 hours to a timestamp

查看:57
本文介绍了向时间戳添加 13 小时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有时间戳值的表,例如:

<前>2009-07-14 02:00:00

我需要在添加 13 小时的运行时显示它们,例如:

<前>2009-07-14 15:00:00

在 PHP 中执行此操作的最简单方法是什么?

解决方案

我知道这个问题很久以前就有人问过了,我已经选择了答案,但我相信将新用户指向更现代和正确的方向很重要,它使用了很棒的 Carbon 库:

<预><代码>>>>Carbon\Carbon::parse('2009-07-14 02:00:00')... -> addHours(13)... -> 格式('Y-m-d H:i:s');=>碳\碳 {#869+"date": "2009-07-14 15:00:00.000000",+"timezone_type": 3,+"时区": "UTC",}

Carbon 不仅使执行请求的计算变得更简单,而且还可以防止您在手动尝试执行相同操作时可能犯的任何潜在错误.

上述输出示例中的 shell 是 Laravel Tinker.Laravel 附带了 Carbon,所以上面的语句是开箱即用的.如果您没有使用现代 PHP 框架,那么您应该使用.添加碳元素很容易.

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:

    2009-07-14 15:00:00

What's the simplest way to do this in PHP?

解决方案

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 Carbon library:

>>> 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.

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.

这篇关于向时间戳添加 13 小时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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