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?
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.
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
DateTime::add PHP doc
DateInterval::construct PHP doc
You can use strtotime() to achieve this:
$new_time = date("Y-m-d H:i:s", strtotime('+3 hours', $now)); // $now + 3 hours
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
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
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)));
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
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.
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
$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
$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 would like to get the actual date + time, and convert it (the time) to a 24 hour format (e.g: 15/04/2017 13:32:02)
My current code is :
date_default_timezone_set("Asia/Magadan");
$date = date("d/m/Y h:i:s");
echo $date;
That display :
15/04/2017 01:32:02
Thanks.
Put the h capital for 24 hour format date("d/m/Y H:i:s");
Finally, to change to a 24 hour format we only have to change the H of the hour to a MAJ/CAPS.
$date = date("d/m/Y H:i:s");
http://php.net/manual/en/function.date.php
convert (h:i:s) to (H:i:s) . that's worked for me
$date = 10/24/14
$time = 10:10
$mtc = 180 minutes
After using strtotime function i am getting timestamp,
echo strtotime('10/24/14 10:10');
timestamp is 1414138200
How can i add timestamp to 180 minutes. Can any one please help me.
If i'm understand your question well, you just can add the seconds to the current timestamp.
$timestamp = time();
$timestamp = $timestamp + 60*180; //180 minutes later
Unix timestamp is expresses in seconds. So to add 180 mins to it, just convert to secs and add to the current timestamp like
$ts = strtotime('10/24/14 10:10');
$ts += 180*60;
echo date("m/d/Y H:i",$ts);
Try this on
$time = strtotime('10/24/14 10:10');
$afterAddMinutesTime = strtotime('+180 minutes', $time);
echo $afterAddMinutesTime; // this is new timestamp after add 180 minutes
echo date('Y-m-d H:i:s', $afterAddMinutesTime); // just for checking
I have the current date format $date = 'yyyy-mm-dd hh:mm' and notice that $date is already set and not using the Date class. I understand I have to use strtotime which I have done to my $date $datestr = strtotime($date). I wish to subtract 5 minutes to the set time.
I've tried this simple thing $A = strtotime($date) - strtotime('-5 min'); But it does not aid me.
its simple to subtract the seconds of 5 minutes which is (5*60)=300 from your time
like this
$time = strtotime($date);
$time_new = $time-(5*60); // will time of the -5 min from the current $time
example
$date = date('d-M-Y g:i:s A'); // current date
echo $date."<br/>"; // output : 17-Feb-2014 8:35:58 AM
$time = strtotime($date); // convert current date to timestamp
$time_new = $time - (5*60); // subtract 5 minutes from current time.
$date_new = date('d-M-Y g:i:s A', $time_new); // convert time to the date again
echo $date_new; // output : 17-Feb-2014 8:30:58 AM
I have the data 2013-02-04 03:20:00
How do I arrive into this 07:20 using php.
Same is through with this 2013-02-04 08:00:00 to this 12:00
I have this data labeled timestamp_diff which is 14400
Any idea? Thank You
I think the answer was base on the timestamp_diff not just adding 4 or any values to it.
Is there any datetime() function(s) to get the equivalent of 14400 to 4
<?php
$add4hour = time() + (4*60*60);
$newTime = date("d m Y H:i:s",$add4hour);
echo $newTime;
?>
Edit because of your requirements
<?php
$add4hour = time() + (4*60*60); // 4 hour adding
$d1 = date("d-m-Y H:i:s"); // date 1 assume that hour 03:20:00
$d2 = date("d-m-Y H:i:s",$add4hour); // date 2, Assume that hour 07:20:00 by adding 4hour
$d1_timestamp = strtotime($d1); // first date's timestamp
$d2_timestamp = strtotime($d2); // second date's timestamp
$time_diff = $d2_timestamp - $d1_timestamp; //difference
echo $time_diff; // this will give you 14400
?>
$date = date("Y-m-d H:i:s");
$addhr= 4;//Replace you value to be added here
$format="H:i";
echo $date."<br>";
echo date($format, strtotime("$date + $addhr hours"));
You can use \DateTime class and add any interval with DateTime::add. Also you can use procedure style.