添加“ x”到日期的小时数

我现在让 php 返回当前的日期/时间,如下所示:

$now = date("Y-m-d H:m:s");

我想做的是有一个新的变量 $new_time等于 $now + $hours,其中 $hours是一个从24小时到800小时的小时数。

有什么建议吗?

224066 次浏览

You may use something like the strtotime() function to add something to the current timestamp. $new_time = date("Y-m-d H:i:s", strtotime('+5 hours')).

If you need variables in the function, you must use double quotes then like strtotime("+{$hours} hours"), however better you use strtotime(sprintf("+%d hours", $hours)) then.

You can use strtotime() to achieve this:

$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours

You can also use the unix style time to calculate:

$newtime = time() + ($hours * 60 * 60); // hours; 60 mins; 60secs
echo 'Now:       '. date('Y-m-d') ."\n";
echo 'Next Week: '. date('Y-m-d', $newtime) ."\n";

Um... your minutes should be corrected... 'i' is for minutes. Not months. :) (I had the same problem for something too.

$now = date("Y-m-d H:i:s");
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours

An other solution (object-oriented) is to use DateTime::add

Example:

<?php


$now = new DateTime(); //now
echo $now->format('Y-m-d H:i:s'); // 2021-09-11 01:01:55


$hours = 36; // hours amount (integer) you want to add
$modified = (clone $now)->add(new DateInterval("PT{$hours}H")); // use clone to avoid modification of $now object
echo "\n". $modified->format('Y-m-d H:i:s'); // 2021-09-12 13:01:55

Run script


You can try lib Ouzo goodies, and do this in fluent way:

echo Clock::now()->plusHours($hours)->format("Y-m-d H:m:s");

API's allow multiple operations.

Correct

You can use strtotime() to achieve this:

$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', strtotime($now))); // $now + 3 hours
$date_to_be-added="2018-04-11 10:04:46";
$added_date=date("Y-m-d H:i:s",strtotime('+24 hours', strtotime($date_to_be)));

A combination of date() and strtotime() functions will do the trick.

   $now = date("Y-m-d H:i:s");
date("Y-m-d H:i:s", strtotime("+1 hours $now"));

I use this , its working cool.

//set timezone
date_default_timezone_set('GMT');


//set an date and time to work with
$start = '2014-06-01 14:00:00';


//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));

I like those built-in php date expressions like +1 hour, but for some reason, they fall out of my head all of the time. Besides, none of the IDEs I'm aware of suggest auto-completion facility for that kind of stuff. And, finally, although juggling with those strtotime and date functions is no rocket science, I have to google their usage each time I need them.

That's why I like the solution that eliminates (at least mitigates) those issues. Here's how adding x hours to a date can look like:

(new Future(
new DateTimeFromISO8601String('2014-11-21T06:04:31.321987+00:00'),
new NHours($x)
))
->value();

As a nice bonus, you don't have to worry about formatting the resulting value, it's already is ISO8601 format.

This example uses meringue library, you can check out more examples here.

For a given DateTime, you can add days, hours, minutes, etc. Here's some examples:

$now = new \DateTime();


$now->add(new DateInterval('PT24H')); // adds 24 hours


$now->add(new DateInterval('P2D')); // adds 2 days

PHP: DateTime::add - Manual https://www.php.net/manual/fr/datetime.add.php

for add 2 hours to "now"

$date = new DateTime('now +2 hours');

or

$date = date("Y-m-d H:i:s", strtotime('+2 hours', $now)); // as above in example

or

$now = new DateTime();


$now->add(new DateInterval('PT2H')); // as above in example

$to = date('Y-m-d H:i:s'); //"2022-01-09 12:55:46"

$from = date("Y-m-d H:i:s", strtotime("$to -3 hours")); // 2022-01-09 09:55:46