How can i change unix timestamp to date in php?

use date function date [ string $format [, int $timestamp = time[] ] ]

Use date['c',time[]] as format to convert to ISO 8601 date [added in PHP 5] - 2012-04-06T12:45:47+05:30

use date["Y-m-d\TH:i:s\Z",1333699439] to get 2012-04-06T13:33:59Z

Here are some of the formats date function supports


The epoch time or Unix timestamp is used to describe a point in time. It is a number of seconds between date time which elapsed since 00:00:00 Thursday, 1 January 1970 at Coordinated Universal Time [UTC]. You have used unix timestamp converter or epoch converter tools online. These tools convert unix timestamp in seconds or milliseconds to human readable date.

So if you’re also thinking about making Unix timestamp conversion tools using PHP, then you’re here at right place. In this tutorial, you will learn how to implement Unix Timestamp conversion to human readable date using PHP. You will also learn how to display GMT date time according to particular timezone using PHP.

Also, read:

  • Build Invoice System with PHP & MySQL
  • Build Live Chat System with Ajax, PHP & MySQL
  • Build Comment System with Ajax, PHP & MySQL

We will cover this tutorial in easy steps with live demo to convert unix timestamp to date with PHP.

So let’s start the coding

Step1: Convert Unix Timestamp to PHP DateTime
First we will get unix timestamp and then convert it into PHP date time using PHP DateTime class.

$unix_timestamp = $_POST['timestamp'];
$datetime = new DateTime["@$unix_timestamp"];

Step2: Display PHP Date Time in Formatted Form
Now we will display PHP Date Time in formatted form like “02-10-2016 21:05:18”.

echo $datetime->format['d-m-Y H:i:s'];

Step3: Display PHP Date Time with Specific Timezone
Now we will display PHP date time from UTC time to specific timeozone.

$date_time_format = $datetime->format['Y-m-d H:i:s'];
$time_zone_from="UTC";
$time_zone_to='Asia/Kolkata';
$display_date = new DateTime[$date_time_format, new DateTimeZone[$time_zone_from]];
$display_date->setTimezone[new DateTimeZone[$time_zone_to]];
echo $display_date->format['d-m-Y H:i:s']

Step4: Complete Example Code
Here is complete code to display human readable date time from Unix Timestamp with timezone.

$unix_timestamp = $_POST['timestamp'];
$datetime = new DateTime["@$unix_timestamp"];
// Display GMT datetime
echo $datetime->format['d-m-Y H:i:s'];
$date_time_format = $datetime->format['Y-m-d H:i:s'];
$time_zone_from="UTC";
$time_zone_to='Asia/Kolkata';
$display_date = new DateTime[$date_time_format, new DateTimeZone[$time_zone_from]];
// Date time with specific timezone
$display_date->setTimezone[new DateTimeZone[$time_zone_to]];
echo $display_date->format['d-m-Y H:i:s']

You will also like these tutorials:

  • Star Rating System with Ajax, PHP and MySQL
  • Create Event Calendar with jQuery, PHP and MySQL
  • Build Your Own CAPTCHA Script with PHP
  • Convert Unix Timestamp To Readable Date Time in PHP
  • Ajax Drop Down Selection Data Load with PHP & MySQL
  • Inventory Management System with Ajax, PHP & MySQL
  • Drag and Drop File Upload using jQuery and PHP
  • Create Live Editable Table with jQuery, PHP and MySQL
  • Live Add Edit Delete datatables Records with Ajax, PHP and MySQL
  • Stripe Payment Gateway Integration in PHP
  • Export Data to Excel with PHP and MySQL
  • Star Rating System with Ajax, PHP and MySQL
  • Create Dynamic Bootstrap Tabs with PHP & MySQL
  • How To Create Simple REST API in PHP

You can view the live demo from the Demo link and can download the script from the Download link below.
Demo Download

How do I convert Unix timestamp to current date?

To easily convert UNIX timestamp to date in the . csv file, do the following: 1. =R2/86400000+DATE[1970,1,1], press Enter key.

Which function returns the Unix timestamp for a date in PHP?

The strtotime[] function parses an English textual datetime into a Unix timestamp [the number of seconds since January 1 1970 00:00:00 GMT].

How do I convert timestamp to date?

The constructor of the Date class receives a long value as an argument. Since the constructor of the Date class requires a long value, we need to convert the Timestamp object into a long value using the getTime[] method of the TimeStamp class[present in SQL package].

How do I convert epoch to date?

Convert from epoch to human-readable date String date = new java.text.SimpleDateFormat["MM/dd/yyyy HH:mm:ss"].format[new java.util.Date [epoch*1000]]; Epoch in seconds, remove '*1000' for milliseconds. myString := DateTimeToStr[UnixToDateTime[Epoch]]; Where Epoch is a signed integer.

Chủ Đề