I have the following php code that is meant to format the current date and time:
$rawdatetime = time();
$datetime = date('Y-m-d', $rawdatetime) . 'T' . date('H:i:s', $rawdatetime) . '.000Z';
$this->debug($datetime);
The formatting seems to be working fine, but it keeps outputting that it is 1970; I get the following output:
1970-01-01T00:00:00.000Z
My guess is that my server is not configured properly, but my Google search has not given me any clues. I am running WAMP if it helps.
Thanks in advance for any advice you may have.
EDIT: it seems that the date and time functions are working properly; but assigning them to a variable is what is the problem. Any work arounds to get the same formatting as above would be welcomed. But I would also like to know why this problem is happening.
Firstly, you don't need to use time()
at all here, because date()
will use the current time as the default value if you don't pass a value to that parameter.
Secondly, you're using two separate date()
calls, separated by a "T"
. Note that the formatting for date()
can accept a hard-coded character like T
; you just need to escape them with backslashes, so you don't need to split it into two function calls.
Your entire code could look like this:
$datetime = date('Y-m-d \T H:i:s.\0\0\0\Z');
Which gives 2013-06-19 T 11:18:53.000Z
It works perfectly for me (when echoing, instead of $this->debug
). So either you somehow have a faulty PHP version, or the problem is not in your code sample. This is what I did:
<?php
$rawdatetime = time();
$datetime = date('Y-m-d', $rawdatetime) . 'T' . date('H:i:s', $rawdatetime) . '.000Z';
echo $datetime,"
";