The problem is here:
$current_date = time();
$now = new DateTime($current_date);
The value returned by time()
is the number of seconds since 1970-01-01 00:00:00 UTC
. The DateTime
constructor tries to interpret it as a date that uses one of the usual date formats, it fails and produces a DateTime
objects initialized with 0
(i.e. 1970-01-01 00:00:00 UTC
).
If you want to create a new DateTime
object from an Unix timestamp (the value returned by time()
you can use DateTime::createFromFormat()
$current_time = time();
$now = DateTime::createFromFormat('U', $current_time);
Or you can pass the timestamp prefixed with '@'
to DateTime::__construct()
:
$current_time = time();
$now = new DateTime('@'.$current_time);
This format is explained in the Compound date/time formats page.
But the easiest way to create a DateTime
object that contains the current date and time is to either pass 'now'
as argument to the constructor or omit it altogether:
$now1 = new DateTime('now');
$now2 = new DateTime();
The two DateTime
objects constructed above should be identical (there is a small chance they are 1-second apart, though) and they both must contain the current date & time.