在PHP中添加小时到日期和时间会产生错误的输出
问题描述:
$datetime = Carbon::now()
$datetime = date($datetime);
$date = strtotime($datetime);
$date = strtotime($datetime);$dispatchDateTime = date("Y-m-d H:m:s", strtotime("+2 hours", $date));
Is this correct.. actully i am gettig wrong output. for any time it just give time like 2016-07-13 14:07:43 or 2016-07-13 13:07:43 time
$ datetime = Carbon :: now()
$ datetime = date($ datetime); \ n $ date = strtotime($ datetime);
$ date = strtotime($ datetime); $ dispatchDateTime = date(“Ymd H:m:s”,strtotime(“+ 2小时”,$ date));
code> pre>
这是正确的..实际上我是错误的输出。 任何时候它只是给时间像2016-07-13 14:07:43或2016-07-13 13:07:43时间 p>
div>
答
$date = new DateTime('now');
$twoHoursLater = $date->add(DateInterval::createFromDateString('+2 hours'));
Hope this helps.
Best Regards,
M.I.
答
Since you are using carbon
why not utilizing it to get what you want to achieve. Notice that I added the addHours()
function here.
Try the code below and learn from it.
$dt = Carbon::create(2012, 1, 31, 0);
echo $dt->toDateTimeString();
echo $dt->addHours(24);
echo $dt->addHour();