How to Change the Date Format in PHP
PHP is a general-purpose open-source scripting language that is optimized for Web development. PHP comes bundled with a number of functions that programmers can use to implement common tasks, such as those that require dates and timestamps. PHP has functions that allow programmers to create dates and times using many standard formats and PHP provides a wide assortment of built-in formats capable of displaying dates and times in virtually any format the user interface or program requires.
Instructions
-
-
1
Open a text editor and create a new file named dateFormats.php. Insert an open "<?php" PHP script delimiter and a close "?>" PHP script delimiter. Any text placed between these delimiters is considered PHP code and interpreted by the PHP program. Save dateFormats.php on the Web server.
<?php
?>
-
2
Create a variable between the "<?php" and "php>" tags named "$thisDate." Use the strtotime() PHP function to parse the date "7/30/70" into a Unix timestamp relative to now. Set the $thisDate variable to the timestamp.
<?php
$thisDate = strtotime("7/30/1970");
?>
-
-
3
Use the PHP "echo" language construct to print an HTML line break ("") tag. Declare a variable named $dateFormat1 and assign a "date()" function to the variable. Pass the format 'm-d-Y' and the variable $thisDate to the date() function. The 'm-d-Y' format indicates that $thisDate should be displayed as a single month, a single day, and a four-digit year and that these values should be separated with a dash ("-").
<?php
$thisDate = strtotime("7/30/1970");
echo "";
$dateFormat1=date('m-d-Y', $thisDate);
?>
-
4
Use the PHP echo language construct to print the text "Date with month, day and full year formatted with dashes: " to the page. Print the value of the $dateFormat1 variable.
<?php
$thisDate = strtotime("7/30/1970");
echo "";
$dateFormat1=date('m-d-Y', $thisDate);
echo "Date with month, day and full year formatted with dashes: ";
echo $dateFormat1;
?>
-
5
Use the PHP echo() language construct to output an HTML line break "()" tag.
<?php
$thisDate = strtotime("7/30/1970");
echo "";
$dateFormat1=date('m-d-Y', $thisDate);
echo "Date with month, day and full year formatted with dashes: ";
echo $dateFormat1;
echo "";
?>
-
6
Declare a variable named $dateFormat2 and assign a date() function to the variable. Pass the format 'M-d-y' and the variable $thisDate to the date() function. The 'M-d-y' format indicates that $thisDate should be displayed as a month abbreviation, a single day and a two-digit year and that these values should be separated with a dash ("-").
<?php
$thisDate = strtotime("7/30/1970");
echo "";
$dateFormat1=date('m-d-Y', $thisDate);
echo "Date with month, day and full year formatted with dashes: ";
echo $dateFormat1;
echo "";
$dateFormat2=date('M-d-y', $thisDate);
?>
-
7
Use the PHP echo language construct to print the text "Date with month abbreviation, day and two-digit year formatted with dashes: " to the page. Print the value of the $dateFormat2 variable.
<?php
$thisDate = strtotime("7/30/1970");
echo "";
$dateFormat1=date('m-d-Y', $thisDate);
echo "Date with month, day, and full year formatted with dashes: ";
echo $dateFormat1;
echo "";
$dateFormat2=date('M-d-y', $thisDate);
echo "Date with month abbreviation, day and two-digit year formatted with dashes: ";
echo $dateFormat2;
?>
-
8
Use the PHP echo language construct to output an HTML line break "()" tag.
<?php
$thisDate = strtotime("7/30/1970");
echo "";
$dateFormat1=date('m-d-Y', $thisDate);
echo "Date with month, day and full year formatted with dashes: ";
echo $dateFormat1;
echo "";
$dateFormat2=date('M-d-y', $thisDate);
echo "Date with month abbreviation, day and two-digit year formatted with dashes: ";
echo $dateFormat2;
echo "";
?>
-
9
Declare a variable named $dateFormat3 and assign a date() function to the variable. Pass the format 'Y/m/d' and the variable $thisDate to the date() function. The 'Y/m/d' format indicates that $thisDate should be displayed as a four-digit year, a month and a day and that these values should be separated with a forward-slash ("/").
<?php
$thisDate = strtotime("7/30/1970");
echo "";
$dateFormat1=date('m-d-Y', $thisDate);
echo "Date with month, day and full year formatted with dashes: ";
echo $dateFormat1;
echo "";
$dateFormat2=date('M-d-y', $thisDate);
echo "Date with month abbreviation, day and two-digit year formatted with dashes: ";
echo $dateFormat2;
echo "";
$dateFormat3=date('Y/m/d', $thisDate);
?>
-
10
Use the PHP echo() language construct to print the text "Date with full year, month and day formatted with forward-slashes: ". Print the value of the $dateFormat3 variable using the php echo() function.
<?php
$thisDate = strtotime("7/30/1970");
echo "";
$dateFormat1=date('m-d-Y', $thisDate);
echo "Date with month, day and full year formatted with dashes: ";
echo $dateFormat1;
echo "";
$dateFormat2=date('M-d-y', $thisDate);
echo "Date with month abbreviation, day and two-digit year formatted with dashes: ";
echo $dateFormat2;
echo "";
$dateFormat3=date('Y/m/d', $thisDate);
echo "Date with full year, month and day formatted with forward-slashes: ";
echo $dateFormat3
?>
-
11
Use a Web browser to open dateFormats.php. Verify that the dates are formatted and written to the Web page as expected.
-
1
Tips & Warnings
PHP 5.1.1 and forward offer constants that can save time when formatting dates, including DATE_RSS, DATE_ATOM, and DATE_W3C.
The strtotime() function allows the creation of relative dates.
The mktime() function can be used to display and format past and future dates.
The getdate() function can be used to create an array that contains date information.
The strtotime() function uses the TZ environment variable to calculate the timestamp. For information on setting the system time zone, refer to the resources section.
References
Resources
- Photo Credit Comstock Images/Comstock/Getty Images