How to Convert a Year Into Two Digits in PHP
PHP is a server-side scripting language that comes with many pre-installed features and functions for manipulating dates and times. Because it stores dates internally as 64-bit numbers, PHP supports all imaginable dates. PHP makes wide use of Unix timestamps in date and time implementations, where a timestamp is the number of seconds that have passed since the Unix epoch (1970). PHP also has a variety of predefined format parameters that can be used when working with dates to customize programs and user interfaces.
Instructions
-
-
1
Open a text editor and create a new file named "digitDate.php". Add some HTML tags to the file that comprise a basic HTML page. Place two PHP delimiters ("<php" and "?>") between the file's "<body>" and "</body>" tags. Any text placed between these two delimiters will be interpreted as code by the PHP program.
<html>
<head></head>
<body>
<?PHP
?>
</body>
</html>
-
2
Declare a PHP variable named "$fourDigitYear". Use the PHP function "strtotime()" to return the Unix timestamp for the date "1/1/2005" and store the Unix timestamp in the "$fourDigitYear" variable.
<html>
<head></head>
<body>
<?PHP
$fourDigitYear = strtotime("1/1/2005");
?>
</body>
</html>
-
-
3
Use the PHP "print()" function to print the Unix timestamp. The timestamp represents the number of seconds since Jan. 1, 1970, relative to the provided date ("1/1/2005"). Include the text " Unix Timestamp: " and the PHP concatenation operator ("."), followed by the "$fourDigitYear" variable.
<html>
<head></head>
<body>
<?PHP
$fourDigitYear = strtotime("1/1/2005");
print " UNIX Timestamp: " . $fourDigitYear;
?>
</body>
</html>
-
4
Use the PHP "print()" function to print the formatted two-digit year based on the "$fourDigitYear" variable. To print the formatted date, use the PHP "date()" function and specify the "y" format parameter string and the "$fourDigitYear" timestamp. Include the text " Converted Year: " and the PHP concatenation operator ("."), followed by the "date()" function.
<html>
<head></head>
<body>
<?PHP
$fourDigitYear = strtotime("1/1/2005");
print " UNIX Timestamp: " . $fourDigitYear;
print " Converted Year: " . date("y",$fourDigitYear);
?>
</body>
</html>
-
5
Open digitDate.php in a Web browser. Verify that the Unix timestamp prints to the Web page followed by the timestamp converted to a two-digit year.
-
1
Tips & Warnings
The PHP date() function's second argument is optional. If this argument (the timestamp) is omitted, the function will use the value of the time() function.
PHP offers a variety of format parameters that can be used with the date() function. A link to a complete list of these parameters is provided in the resources section.
In addition to format parameters, PHP has a variety of relative parameters. See the resources section for a list of relative parameters.
Date and time functions are dependent on the local server settings. Be sure to consider daylight savings and leap years when using these functions.
References
Resources
- Photo Credit Thinkstock/Comstock/Getty Images