-2

i am working on a Cakephp 2.x ..i am getting a date in a UNIX timestamp ..i want to convert this Unix timestamp into GMT and then want to save into the database in GMT format .. don't know how to do that neither in cakephp nor in php .. if some one knows how to do that please share me the code .. i have seen the documentation of Cakephp but not able to understand this ... so please if you want to paste the link of documentation then please don't answer. if someone don't know how to do this in cake but knows about in php then its OK

hellosheikh
  • 2,929
  • 8
  • 49
  • 115
  • @AmalMurali i didnt try yet ... dont know how to start ... i am posting data from android in which i am getting a date in a variable in a unix timestamp in webapp .. so dont know what to do – hellosheikh Jul 01 '13 at 17:27

1 Answers1

0

I wrote you a function that will convert the timestamp to the format MySQL expects for DateTime fields in GMT timezone.

function format_timestamp($unixtimestamp, $format='Y-m-d H:i:s', $timezone = 'GMT'){
    $DateTime = new DateTime;
    $DateTime->setTimestamp($unixtimestamp);
    $DateTimeZone = new DateTimeZone($timezone);
    $DateTime->setTimezone($DateTimeZone);
    return $DateTime->format($format);
}

echo format_timestamp(time());
azBrian
  • 661
  • 1
  • 5
  • 19