How can I get a date in PHP with this format?
1976-03-06T23:59:59.999Z
I want the current date + 10 hours in that format. I have tried this:
date("Y-m-d",strtotime("+10 hours"));
but I fail to see how to get that format.
Thanks!
How can I get a date in PHP with this format?
1976-03-06T23:59:59.999Z
I want the current date + 10 hours in that format. I have tried this:
date("Y-m-d",strtotime("+10 hours"));
but I fail to see how to get that format.
Thanks!
To get precisely what you are looking for you will need to do the following:
Set the PHP timezone to ensure that regardless of your server or PHP timezone, the time output will be in correct zone (in your case 'Z').
date_default_timezone_set('UTC');
Then compute the time you need (current time plus 10 hours);
$timestamp = time() + (10 * 60 * 60); // now + 10 hours * 60 minutes * 60 seconds
Then convert to a formatted date.
If you are not concerned about the seconds and milliseconds, then use PHP's inbuilt function for ISO 8601 dates.
echo date('c', $timestamp); // Will output 1976-03-06T23:59Z
else you will need to determine the current microseconds and assemble the date string manually.
// Get current timestamp and milliseconds
list($microsec, $timestamp) = explode(" ", microtime());
// reduce microtime to 3 dp
$microsec = substr($microsec,0,3);
// Add 10 hours (36,000 seconds) to the timestamp
$timestamp = (int)$timestamp + (10 * 60 * 60);
// Construct and echo the date string
echo date('Y-m-d', $timestamp) . 'T' . date('H:i:s', $timestamp) . '.' . $microsec . 'Z';
Just add 10 * 60 * 60
seconds to the current time
.
date('c', time() + 10 * 60 * 60);
This is pretty close to what you're looking for.
date('c')
// prints 2013-01-03T18:39:07-05:00
As others have said, check the documentation to make something more customized.