1

I'm working with the SparkPost API and I am having trouble understanding how to create a date with YYYY-MM-DDTHH:MM:SS+-HH:MM in PHP.

What is +-HH:MM, is that the timezone?

The closest I have come from reviewing other similar stack exchange topics is this:

if ( !$send_now && self::$row->datetime )
{
   // This data is available: [send_datetime] => 04/01/2016 01:30
   // This data is available: [timezone] => MDT-UTC-7
   $send_at = date( 'Y-m-dTH:i:s' , strtotime( self::$row->datetime ) );
}
else
{
   $send_at = 'now';
}

Another FYI is I am working with WordPress and will want to target the time zone the instance is set to.

Any help appreciated.


updated code that's working
if ( !$send_now && self::$row->datetime ) {
    $send_at = date( 'c' , strtotime( self::$row->datetime ) );
    if (isset(self::$email_settings['timezone'])) {
        $date_parts = explode('+' , $send_at );
        $timezone_parts = explode('UTC' , self::$email_settings['timezone'] );
        $send_at = $date_parts[0] .$timezone_parts[1].':00';
    }
} else {
        $send_at = 'now';
}
atwellpub
  • 5,660
  • 11
  • 38
  • 52

2 Answers2

4

This is an ISO 8601 date format. See https://en.wikipedia.org/wiki/ISO_8601.

The +-HH:MM is the timezone offset from UTC.

In PHP you can use 'c' to format a date as ISO 8601.

$send_at = date( 'c', strtotime( self::$row->datetime ) );
Michael
  • 1,643
  • 1
  • 15
  • 31
  • Is there a trick to set the timezone correctly when all I have are strings like 'MDT-UTC-7' (I believe that's Mountain Time) and 'CDT-UTC-5' for Central? I could parse the last part of the string, but is PHP setup to interpret these shorthand strings? – atwellpub Apr 22 '16 at 22:57
  • 1
    You could use `timezone_name_from_abbr` to get the timezone using all or part of these strings like `$timezone = timezone_name_from_abbr('CDT-UTC-5')`. You might have to parse out the first three letters e.g. CDT. You could then pass the timezone in when creating the date like `$date = new DateTime('04/01/2016 01:30', new DateTimeZone($timezone));`. Then output it as ISO 8601 using `$date->format('c')`. – Michael Apr 22 '16 at 23:19
3

This is the format you are looking for

$send_at = date( 'c' , strtotime( self::$row->datetime ) );

its called the ISO 8601 date

Output from a UTC location is 2016-04-22T22:48:10+00:00

And yes the +-HH:MM is the timezone diff from UTC, either + or -

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149