问题
I currently have php returning the current date/time like so:
$now = date(\"Y-m-d H:m:s\");
What I\'d like to do is have a new variable $new_time
equal $now + $hours
, where $hours
is a number of hours ranging from 24 to 800.
Any suggestions?
回答1:
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.
回答2:
An other solution (object-oriented) is to use DateTime::add
Example:
$now = new DateTime(); //current date/time
$now->add(new DateInterval("PT{$hours}H"));
$new_time = $now->format('Y-m-d H:i:s');
PHP documentation.
回答3:
You can use strtotime() to achieve this:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
回答4:
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
回答5:
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";
回答6:
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
回答7:
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.
回答8:
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)));
回答9:
$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.
回答10:
I use following function to convert normal date-time value to mysql datetime format.
private function ampmtosql($ampmdate) {
if($ampmdate == '')
return '';
$ampm = substr(trim(($ampmdate)), -2);
$datetimesql = substr(trim(($ampmdate)), 0, -3);
if ($ampm == 'pm') {
$hours = substr(trim($datetimesql), -5, 2);
if($hours != '12')
$datetimesql = date('Y-m-d H:i',strtotime('+12 hour',strtotime($datetimesql)));
}
elseif ($ampm == 'am') {
$hours = substr(trim($datetimesql), -5, 2);
if($hours == '12')
$datetimesql = date('Y-m-d H:i',strtotime('-12 hour',strtotime($datetimesql)));
}
return $datetimesql;
}
It converts datetime values like,
2015-06-04 09:55 AM -> 2015-06-04 09:55
2015-06-04 03:55 PM -> 2015-06-04 15:55
2015-06-04 12:30 AM -> 2015-06-04 00:55
Hope this will help someone.
回答11:
$now = date("Y-m-d H:i:s");
date("Y-m-d H:i:s", strtotime("+1 hours $now"));
来源:https://stackoverflow.com/questions/11386308/add-x-number-of-hours-to-date