Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get next Date with specific time in PHP

Tags:

php

datetime

I want to get the date with specific day and time in PHP, like i want the date of next day and time of 9.30 am i.e "2011-06-02 09:30:00". the code i was using get to do that,

<?php
   $next_day_date = date("Y")."-".date("m")."-".(date("d")+1)."  09:30:00";
   $new_trig_time_stamp = strtotime($next_day_date);
   $trigger_date_time = date("Y-m-d H:i:s",$new_trig_time_stamp);
   echo $trigger_date_time;
?>

the code above works fine but fails on 31 day, on 31st it returns "1970-01-01 05:30:00". Is there any other way to do so.

like image 958
Harish Kurup Avatar asked Dec 21 '22 13:12

Harish Kurup


1 Answers

When shifting dates by a fixed number, it's better to use mktime(), because it handles invalid dates well (e.g. it knows that January 32 is in fact February 1)

$trigger_date_time = date("Y-m-d H:i:s", mktime(9,30,0, date('n'), date('j')+1, date('Y'));
like image 66
mkilmanas Avatar answered Jan 09 '23 17:01

mkilmanas