I am getting ISO datetime using below code
$date= date("c");
echo $date;
which returns something like below
2016-01-07T20:18:46+00:00
But the API I use tells that its wrong format and it needs in below format:
2016-01-07T20:35:06+00Z
I need to remove :00
at the end and add Z
.
I am completely new to regex , can anyone help me understand the regex and tell which format is required.
You'll want to define the date format specifically.
If microseconds will always be 00
date("Y-m-d H:i:s+00\Z");
Else, use this little big of logic
date("Y-m-d H:i:s+"). substr((string)microtime(), 2, 2) . 'Z';
More info.
You can find the last occurrence of :
with strrpos
, and get the substring up to it with substr
, and then add Z
:
$date= date("c");
echo "Current: $date
"; // => 2016-01-07T21:58:08+00:00
$new_date = substr($date, 0, strrpos($date, ":")) . "Z";
echo "New : " . $new_date; // => 2016-01-07T22:10:54+00Z
See demo