Speed up development with full-stack environments for every branch.

Learn More

Date formatting with date() function [PHP]

242 Runs 1697 Views 142 Copies
Saved

Saved

sundip 66

sundip
published 4 years ago

<?php

/**
 * Date formatting with date() function
 * 
 */
//get the time in microseconds from a valid string date
$current_time = strtotime('24-01-2014 12:35:22');

//using different formats to ilustrate function usage
$date_formats=array('4 digits year - numeric month with leading zeros - numeric day with leading zeros'
=>'Y-m-d',
'4 digits year - numeric month with leading zeros - numeric day with leading zeros hour with leading 
zeros:minute with leading zeros:seconds with leading zeros'
=>'Y-m-d H:i:s',
'Full textual day, numeric day with leading zeros full textual month 4 digit year'
=>'l, d  F  Y',
'3 letters day 3 letters month day of the month without leading zeros 24 hours format with leading 
zeros:minutes with leading zeros:seconds with leading zeros timezone abreviation 4 digits year'
=>'D M j G:i:s T Y');
echo '<center>';
foreach ($date_formats as $formatName=>$formatString){
    //formatting the date warning: time to be converted must be in microseconds 
    //and you can convert almost any date format using php function strtotime() 
    //or you can generate it with mktime()
    $dateFormated=date($formatString,$current_time);
echo '<br /><div style="background-color:green;color:#fff;padding:10px;width:600px;font-size:16px">
<b>Date Format:</b><br />' . $formatName . '
<br /><br />
<b><u>date(\''.$formatString.'\', '.$current_time.') output:</u></b> '.$dateFormated.' 
    </div><br />';
}
//format the date without using the second parameter wich will output the current time formated as a date
$dateFormated=date('d-m-Y');
echo '<br /><div style="background-color:yellow;color:#000;padding:10px;width:600px;font-size:16px">
<b>You can use date() only with the formatting string and you will get the current server date formated</b>
<br /><br /><br /><b><u>date(\'d-m-Y\') output:</u></b> '.$dateFormated.' 
    </div><br />';
echo '</center>';
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).