atwellpub atwellpub - 7 months ago 39
PHP Question

Creating this date format in PHP: YYYY-MM-DDTHH:MM:SS+-HH:MM

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';
}

Answer

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 ) );