-1

I am working on my php script to set up the date with the time. I need some help with convert the day date to the current day and next day date, example: my current time is 15:27 and my current date is 27-11-2019 so when I have the string for the variable get_time1 is 06:00:00, I want to convert it to 28-11-2019 06:00:00. When I have the variable get_time2 that have the time which it is 23:00:00 as my current time is before 23:00:00 so i want to convert the date with the current date with the time to 27-11-2019 23:00:00.

Code:

<?php

$get_time1 = '06:00:00';
$get_time2 = '23:00:00';

date_default_timezone_set('Europe/London');

$Date = date('Y-m-d');
$time = date('H:i:s');

?>

Can you please show me an example how I can set up the day date with the time 06:00:00 and 23:00:00 as if the time 06:00:00 is after 12am to set up the next day date and if the time 23:00:00 is before 12am then set up the time with the current date?

Thank you.

3 Answers3

0

If you use a DateTime that will allow you to do date arithmetic.

$now = new DateTime();
$tomorrow = $now->modify("+1 day");

You can also use strtotime to get a unix timestamp as explained in this answer.

$tomorrow = strtotime('+1 day');
cjc
  • 731
  • 3
  • 13
0

This just creates a DateTime object from the time (which will default it to todays date) and if this is less than the current date and time, it adds 1 day...

$date = DateTime::createFromFormat("H:i:s", $get_time2);

if ( $date < new DateTime() )   {
    $date->modify("+1 day");
}

which gives

2019-11-27 23:00:00

and for $get_time1...

2019-11-28 06:00:00
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55
  • thank you very much for this, in your code that you wrote if I have the time `06:00:00`, how i can add it to the next day and when I have the time `23:00:00` how I can use the current day date with the time? I want to use 2 different times as i can set up the date to send the emails when the time is passed. –  Nov 27 '19 at 16:20
  • Sorry - I've changed the code - this checks that the time is in the future now (using `<`) – Nigel Ren Nov 27 '19 at 16:22
0

maybe this will do?

$offset = timezone_offset_get( timezone_open( "Europe/London" ), new \DateTime() );
echo 'in London' . gmdate('d-m-Y H:i:s', date( "U" )+$offset);

echo 'current location: ' . date('d-m-Y H:i:s', date( "U" ));
kubarik
  • 64
  • 4