Viewed   96 times

Using PHP, I want to convert UNIX timestamps to date strings similar to this: 2008-07-17T09:24:17Z

How do I convert a timestamp such as 1333699439 to 2008-07-17T09:24:17Z?

 Answers

4

Try gmdate like this:

<?php
$timestamp=1333699439;
echo gmdate("Y-m-dTH:i:sZ", $timestamp);
?>
Saturday, October 29, 2022
3

This sounds just like a time zone issue.

A Unix timestamp is always a point in time in UTC.

When displaying it in a format like "Jun 13 2015 8:23AM" you are always using a certain time zone to display it as such. The reason the time is being displayed as "16:23PM" is because a time zone other than you expect is being used to display the Unix timestamp.

So the solution is simply making sure you're picking the correct time zone when displaying the timestamp.

If you're using the JavaScript Date object and you mean to use UTC, you can try using a method like toUTCString():

console.log(new Date(1434183780000).toUTCString());
// Output: Sat, 13 Jun 2015 08:23:00 GMT

Date supports only the local time zone or UTC. If you want to construct your own formatted string in UTC, you can use the methods in Date starting with getUTC*(), like getUTCHours() or getUTCDate() .

See more info on the Date object in: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Monday, October 17, 2022
 
1

strtotime Returns a timestamp on success, FALSE otherwise.

 echo strtotime('2012-07-25 14:35:08' );

Output

1343219708
Tuesday, September 6, 2022
 
4

You can use date() function

$weekday = date('N', $timestamp); // 1-7
$month = date('m', $timestamp); // 1-12
$day = date('d', $timestamp); // 1-31
Wednesday, October 19, 2022
5

PHP uses seconds-based timestamps, so divide 1188604800 by 1000 and you are good.

php> echo date('Y-m-d', 1188604800000/1000);
2007-09-01
Monday, September 5, 2022
 
grinnz
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :