Search code examples
phpdatetimetimezoneutcdatetime-format

Convert php variable into a specified datetime format


From server I get this value: 2014-07-02T05:13:45Z I stored it into a php variable.

$fromserver=$row['today'];

So $fromserver has value '2014-07-02T05:13:45Z' stored in it.

For showing it to users in readable format, I used strtotime() & date() in php.

Here's my code.

date_default_timezone_set('UTC');
$fs = strtotime($fromserver);
$fromserver_formated = date("Y-m-d H:i:s",$fs);

So, now if I echo() $fromserver_formated, I get the date in a readable format.

echo $fromserver_formated; gives 2014-07-02 05:13:45.

Now I want to create a new variable $toserver using the value of $fromserver & $variable.

$variable is an integer variable containing value 7200.

So to calculate to $toserver, I did this.

$ts=$fs+$variable;
$toserver=date('Y-m-d H:i:s',$ts);

And when I echo $toserver, I get 2014-07-02 07:13:45.

And I want to store $toserver into my database. But not in '2014-07-02 07:13:45' this format but in '2014-07-02T07:13:45z' format like $fromserver.

What to do next? Any suggestions?


Solution

  • It's manually added date i think there is not any format to convert .try

    $fromserver = '2014-07-02 05:13:45';
    date_default_timezone_set('UTC');
    $fs = strtotime($fromserver);
    echo $fromserver_formated = date("Y-m-d\TH:i:s\z",$fs); //2014-07-02T05:13:45z