0

I have an string date like this: 2015/12/20 13:58:59

I try to convert timestamp:

$idate = $user->multiexplode(array("/"," ",":"),strip_tags("2015/12/20 13:58:59"));

//mktime( $hour , $minute , $second , $month , $day , $year , $is_dst );
$timestamp = mktime($idate[3],$idate[4],$idate[5],$idate[1],$idate[2],$idate[0]);

And now I try to convert real date:

echo 'new date: '.jdate('Y/n/j H:i:s',$timestamp);

Ok...it works but there is a problem!

according of time server,I get variable time.

for examle for location +1 GMT: 2015/12/20 14:58:59

for -1 GMT: 2015/12/20 11:58:59

I want for all server print 2015/12/20 13:58:59 again

Stefan
  • 5,203
  • 8
  • 27
  • 51
salome
  • 53
  • 4
  • 8

2 Answers2

0

You can use DateTime class and it's methods to convert the date and time to unix timestamp. And use strftime() to convert the unix timestamp back to your desired format, like this:

$datetime = "2015/12/20 13:58:59";

// covert timestamp
$unixdatetime = DateTime::createFromFormat('Y/m/d H:i:s', $datetime)->getTimestamp();
echo $unixdatetime . "<br />";  // 1450616339

// now format the unix timestamp
$formatted_datetime = strftime("%Y/%m/%d, %H:%M:%S",$unixdatetime);
echo $formatted_datetime;  // 2015/12/20, 13:58:59

Output:

1450616339
2015/12/20, 13:58:59

Here are the references:

Rajdeep Paul
  • 16,887
  • 3
  • 18
  • 37
0

Use this code.

<?php

$date = '2015/12/20 13:58:59';

//convert to timestamp
$timestamp = strtotime($date);

echo $timestamp;

//convert timestamp back to datetime
$date_again = date('Y/m/d H:i:s', $timestamp);

echo $date_again;

?>
Alpesh Panchal
  • 1,723
  • 12
  • 9