PHP: calculating time using datetime ()

I am making a time calculator that adds or subtracts days, hours and minutes from the current time to the UTC time zone or user-specified date stored in the MySQL database. My current code works, but I was not able to figure out how to set the time format.

The code:

$days = "+2 days";
$hours = "+1 hours";
$minutes = "+5 minutes";

$time = new DateTime(); // assumes current time
$time->setTimezone(new DateTimeZone('Europe/London'));
$time->modify("$days, $hours, $minutes");
echo $time->format("g:i A l jS F"); // outputs time

Conclusion:

11:48 PM Sunday 4th January

What would be the appropriate formatting to force the interpreter to display the date as "11:48 p.m. Sunday, January 4"? Adding characters such as "N" and "J" will ruin (and / or scramble) the date, even if you escape with one or more "\". Oddly enough, this does not happen with half the alphabet ...

I searched for interwebs but did not find an answer explaining this phenomenon.

Appreciate the help!

+4
1

:

echo $time->format("g:i A \o\\n l \\t\h\\e jS F"); // outputs time
  • \o , ,
  • \\n newline (hex 0A) → escape,
  • \\t tab (hex 09) → escape,
  • \h , ,
  • \\e escape (hex 1B) → escape,

Escape sequences: http://php.net/manual/en/regexp.reference.escape.php

+11

Source: https://habr.com/ru/post/1569766/


All Articles