Convert Unix timestamp into a formatted understandable time string in PHP

By Faruque Ahamed Mollick

Often Unix time format is used in programming related work. But this type of time or date format is not understandable by the human. So it should be converted into a human readable and understandable time format.

In this post, we are going to see how we can convert a Unix timestamp into normal human readable time format easily in PHP.

Calculate the difference between two dates in PHP

In PHP there is gmdate() function is already there. We can use the PHP gmdate function to easily convert our Unix formatted time into any type of time or date format that we would like.

Below is the syntax of PHP gmdate() function:

gmdate(format,timestamp);

Now below is an example code:

<?php
$unix_timestamp = 1521900216;
$formatted_time = gmdate('Y-m-d H:i:s',$unix_timestamp);
echo $formatted_time;
?>

In the above code, we have taken a Unix time and we put it as the parameter in PHP gmdate() function. We have also declared the time format that we want to convert the Unix time into.

If we run the above code then we will able to see the time as below:

2018-03-24 14:03:36

We can get any time format we like by declaring the format in the gmdate() parameter.

So we have converted a Unix timestamp into a formatted, readable time format which can be easily understood.

How To Display A Random Image From Directory In PHP?

 

Leave a Reply

Your email address will not be published. Required fields are marked *