Changing the Format of Date and Time in PHP
In this article, though i am not presenting anything new, but surely something that is quite useful for PHP programmers.
Very usually, we need to get the date and time in a format to suit our needs.
PHP is well enriched with useful functions having appropriate options to make such format conversions.
Code to Get Date and Time in Various Formats Using PHP
<?php
$today = "23.09.2016";
$date = str_replace('.', '-', $today);
echo date('d-m-y', strtotime($date)).'<br/>'; //22-09-16
echo '=======================================================';
echo "<br>";
echo date('d-m-Y', strtotime($date)).'<br/>'; //22-09-2016
echo '=======================================================';
echo "<br>";
echo date('d-M-Y', strtotime($date)).'<br/>'; //22-Sep-2016
echo '=======================================================';
echo "<br>";
echo date('D d-M-Y', strtotime($date)).'<br/>'; //Thu 22-Sep-2016
echo '=======================================================';
echo "<br>";
echo date('D, d M y', strtotime($date)).'<br/>'; //Thu, 22 Sep
echo '=======================================================';
echo "<br>";
echo date("l jS M Y", strtotime($today)).'<br/>'; //Thursday 22nd Sep 2016
echo '=======================================================';
echo "<br>";
echo date("l jS F Y", strtotime($today)).'<br/>'; //Thursday 22nd September 2016
echo '=======================================================';
echo "<br>";
echo date("z ", strtotime($today)).'<br/>'; // 265th day of year
echo '=======================================================';
echo "<br>";
echo date("L ", strtotime($today)).'<br/>'; // 1 if leap year and 0 if not leap a year
echo '=======================================================';
echo "<br>";
$i = 5;
echo date('d-m-Y', strtotime($today. ' + '.$i.' days')).'<br/>'; // date after 5 days
echo '=======================================================';
echo "<br>";
$i = 5;
echo date('d-m-Y', strtotime($today. ' + '.$i.'months')).'<br/>'; // date after 5 months
echo '=======================================================';
echo "<br>";
$i = 5;
echo date('d-m-Y', strtotime($today. ' + '.$i.'years')).'<br/>'; // date after 5 years
echo '=======================================================';
echo "<br>";
echo date("d/m/Y", strtotime($today)).'<br/>';
echo '=======================================================';
echo "<br>";
$orig_date = "22/09/2016";
$date = str_replace('/', '-', $orig_date);
echo date('Y-m-d', strtotime($date)).'<br/>'; // 2016-09-22
echo '=======================================================';
echo "</br>";
echo 'Date with times'.'<br>';
date_default_timezone_set("Asia/Kolkata"); // here you can set your own time zone
echo "current time " . date("h:i:s").'<br/>'; //11:58:05
echo '=======================================================';
echo "</br>";
echo date("h:i:s a").'<br/>'; // 11:58:05 pm
echo '=======================================================';
echo "</br>";
echo date("d M Y, g:i A").'<br/>'; // 22 Sep 2016, 12:03 AM
echo '=======================================================';
echo "</br>";
echo date("G:i:s e").'<br/>'; // 23:58:05 Asia/Kolkata
echo '=======================================================';
echo "</br>";
echo date("H:i:s").'<br/>'; // 00:01:31
echo '=======================================================';
echo "</br>";
echo date("H:i:s P").'<br/>'; // 00:01:31 +0530 ,P tells difference with GMT
echo '=======================================================';
echo "</br>";
echo date("H:i:s T").'<br/>'; // 00:04:59 IST ,T tells timezone
echo '=======================================================';
echo "</br>";
echo date("H:i:s U").'<br/>'; // 00:07:17 1474483037 ,U converts time to UNIX timestamp
echo '=======================================================';
echo "</br>";
echo date("Y-m-d H:i:s").'<br/>'; // 2016-09-22 13:10:07
?>
Check This Also:
PHP Code to Extract Text and Images from a PDF file
PHP Code to Get IP Address and Geo Location of Login User
PHP Code to Convert Numeric Value into Text Format
In the above code, all formatting code structured mentioned. You can use it as per your date and time format using PHP.
Thank you very much for reading this article.
