0

Currently I've used this code to get 1 day started from current timing of today plus 24 hours:

$daysago = date("Y-m-d H:i:s",strtotime(date('Y-m-j H:i:s')) + (1 * 24 * 60 * 60));  //Today + 1 day

How can I fixed the timestamp so for example the $daysago should start from 9am of today until 9am of tomorrow only?

John Conde
  • 217,595
  • 99
  • 455
  • 496
Resolution
  • 759
  • 1
  • 7
  • 18

1 Answers1

0

I'm going to use DateTime() since it is better for working with dates and times. Just create a DateTime object with a 9AM time (which defaults to today's date) and then add a day to it.

 $daysago = (new DateTime('09:00'))->modify('+1 day')->format('Y-m-d H:i:s');

Demo

You can also use DateInterval() to add one day:

 $daysago = (new DateTime('09:00'))->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
John Conde
  • 217,595
  • 99
  • 455
  • 496
  • Sorry for your inconvenience. You can just ignore my previous comment, my concern now is how to make `DATETIME` follow my current country timestamp since it seems like the current `09:00` is not follow `09:00` in my country. How can I make `DATETIME` to follow my country timestamp? – Resolution Jan 06 '15 at 02:42
  • I'm living in Singapore so it's UTC/GMT +8 hours – Resolution Jan 06 '15 at 02:44
  • What timezone is the date stored in? – John Conde Jan 06 '15 at 02:45
  • If the time is stored in the proper timezone you don't need to do anything. This will work as is. – John Conde Jan 06 '15 at 14:04