-2

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!

Hommer Smith
  • 26,772
  • 56
  • 167
  • 296
  • possible duplicate of http://stackoverflow.com/questions/9371457/how-to-subtract-microtime-and-display-date-with-miliseconds-in-php – Gordon Jan 03 '13 at 13:23
  • possible duplicate of [PHP date format converting](http://stackoverflow.com/questions/2332740/php-date-format-converting) – Harald Scheirich Jan 03 '13 at 14:09

3 Answers3

2

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';
PassKit
  • 12,231
  • 5
  • 57
  • 75
1

Just add 10 * 60 * 60 seconds to the current time.

date('c', time() + 10 * 60 * 60);
kmkaplan
  • 18,655
  • 4
  • 51
  • 65
1

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.

Chad
  • 714
  • 2
  • 9
  • 26