2

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.

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
Vishnu
  • 2,132
  • 6
  • 26
  • 52
  • Ok I understood the format , how to change the ISO format in php – Vishnu Jan 07 '16 at 20:32
  • @stribizhev thank you very useful , I understood the format see my edit , also now can you tell how to change to that format in php – Vishnu Jan 07 '16 at 20:41
  • 1
    [Convert date format yyyy-mm-dd => dd-mm-yyyy](http://stackoverflow.com/questions/2487921/convert-date-format-yyyy-mm-dd-dd-mm-yyyy) - there others, tons of such questions. – Wiktor Stribiżew Jan 07 '16 at 20:42
  • not really yyyy mm dd is correct , I need to add remove :00 at and and Z , should i do with str replace or any other default way to do it – Vishnu Jan 07 '16 at 20:48
  • Possible duplicate of [Format of 2013-04-09T10:00:00Z in php](http://stackoverflow.com/questions/15902464/format-of-2013-04-09t100000z-in-php) – War10ck Jan 07 '16 at 22:11
  • 2
    @War10ck: That is not a dupe - there is just no way to use the hours only in *difference to UTC with or without colon between hours and minutes*. I tried myself for about an hour, checking the documentation. [Format of 2013-04-09T10:00:00Z in php](http://stackoverflow.com/questions/15902464/format-of-2013-04-09t100000z-in-php) does not provide the answer for this question since OP needs to keep `+00`. – Wiktor Stribiżew Jan 07 '16 at 22:12

2 Answers2

1

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\n";      // => 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

Wiktor Stribiżew
  • 484,719
  • 26
  • 302
  • 397
1

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.

http://php.net/manual/en/function.date.php

Goose
  • 4,243
  • 3
  • 36
  • 75