PHP provides getdate(), date(), time() functions and DateTime class that can be used to get and format a date and time. The getdate() function stores the current date and time in an associative array. These values can be accessed by using the predefined keys of the array as shown below. The keys allow you get the day, month, year, seconds, minutes, and hours from the array.
|mday||day of month (1-31)||23|
|wday||day of week (0-6)||1|
|weekday||day of week||Monday|
|mon||month of year (1-12)||12|
|month||month of year||December|
echo “Today is ”.$f;
The date() function provides you more control over the format of a date and time. This is the general form of the date() function.
The format string allows you to specify a format of a date and time value. For example, you might want to format the current date as 23-12-2013 10:25:50 AM. The timestamp is an integer value that represents a date and time value. The time() function returns a time stamp of the current date and time. The special characters that can be used in the format string are shown below.
|a||am or pm||10:30:50 am|
|A||AM or PM||10:30:50 PM|
|h||leading zero hours (01-12)||03|
|H||leading zero hours (00-23)||09|
|g||no leading zero hours (1-12)||3|
|G||no leading zero hours (0-23)||9|
|d||leading zero day of month (01-31)||02|
|j||no leading day of month (1-31)||2|
|w||day of week (0-6)||2|
|l||full text day||Thursday|
|m||month of year (01-12)||08|
|M||three-character month of year||Jan|
|F||Full text month of year||January|
echo date("d/m/y h:i:s a",time());
From the above section, you have learnt to get and format the current date and time. Sometimes, however, you might want to format date and time that is not the current date. For example, you have a string that represents a date and want to format to whatever you want. Fortunately, PHP provides the DateTime class that allows you to convert the date string in to a DateTime object. The DateTime class has a method called getTimeStamp() that can be used to get the time stamp from the DateTime object.
echo date("d/m/y h:i:s a",$d->getTimeStamp());
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages: