Convert PHP Timestamp to Date readable format, In this tutorial, we do a complete discussion about all the conversions with help of the example. Here is the list of line items which we discuss further,
- PHP timestamp to date
- PHP UNIX timestamp to date time
- UNIX time live
- PHP convert date to UNIX timestamp
- Convert PHP timestamp to date with specific timezone
Convert PHP Timestamp to Date with Example
With help of PHP date function date('m/d/Y')
we can convert timestamp to human readable date format.
1 2 3 |
<?php echo date('m/d/Y', 1618531200); ?> |
Output:
04/17/2021
Convert PHP UNIX Timestamp to Date Time Both With Example
We use the same as above PHP date function with the help additional time parameter date('m/d/Y H:i:s')
.
1 2 3 |
<?php echo date('m/d/Y H:i:s', 1618531200); ?> |
Output:
04/17/2021 16:30:41
Convert UNIX Timestamp to Live Time
To get live time we PHP time function time()
which is gives current time in UNIX timestamp format.
1 2 3 |
<?php echo date('m/d/Y H:i:s', time()); ?> |
Output:
Current date and time
PHP Convert Date to UNIX Timestamp With Example
Using strtotime()
function we can convert human readable date to UNIX timestamp.
1 2 3 |
<?php echo strtotime('04/16/2021'); ?> |
Output:
1618531200
Convert PHP Timestamp to Date With Specific Timezone
1 2 3 4 5 6 7 |
<?php $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'); ?> |
Also Check Live on https://codingtasks.net/php-online-editor/
Output:
Current date and time of specified zone
17-04-2021 22:04:01
Code Highlights:
- First, we set the origin time zone
$time_zone_from
then define that zone which we want to get the time$time_zone_to
. - Then we use the PHP DateTime object to set the user-defined timezone.
- Now we convert it and print the date-time with specified zone and with the specified format.
This tutorial has complete guide about PHP UNIX timestamp and PHP date conversion opertions.
You can also check more on PHP timestamp and date https://www.php.net/manual/en/datetime.gettimestamp.php
Bonus Points on PHP Date Function
- mm/dd/yyyy:
date("m/d/Y", $current_timestamp);
- dd/mm/yyyy
date("d/m/Y", $current_timestamp);
- With time
date("d F Y H:i:s", $current_timestamp);
- Without time
date("d F Y", $current_timestamp);
Also Check:
- PHP DATE | PHP Date Function
- Get Selected Date From Calendar in PHP using Ajax
- Set DateTime With FileName While Uploading in PHP
- How to Validate Date String in PHP
Happy Coding..!
[…] PHP Timestamp to Date Readable Format With Example […]
[…] PHP Timestamp to Date Readable Format With Example […]
[…] Also Read: PHP Timestamp to Date Readable Format With Example […]