The PHP Show
The PHP Date() FunctionThe PHP Syntax
A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred. Get a DateThe required format parameter of the date() function specifies how to format the date (or time). Here are some characters that are commonly used for dates:
Other characters, like"/", ".", or "-" can also be inserted between the characters to add additional formatting. The example below formats today's date in three different ways: Example<?php Try it Yourself » PHP Tip - Automatic Copyright YearUse the Get a TimeHere are some characters that are commonly used for times:
The example below outputs the current time in the specified format: Note that the PHP date() function will return the current date/time of the server! Get Your Time ZoneIf the time you got back from the code is not correct, it's probably because your server is in another country or set up for a different timezone. So, if you need the time to be correct according to a specific location, you can set the timezone you want to use. The example below sets the timezone to "America/New_York", then outputs the current time in the specified format: Example<?php Try it Yourself » Create a Date With mktime()The optional timestamp parameter in the date() function specifies a timestamp. If omitted, the current date and time will be used (as in the examples above). The PHP Syntaxmktime(hour, minute, second, month, day, year) The example below creates a date and time with the Example<?php Try it Yourself » Create a Date From a String With strtotime()The PHP SyntaxThe example below creates a date and time from the Example<?php Try it Yourself » PHP is quite clever about converting a string to a date, so you can put in various values: Example<?php $d=strtotime("next Saturday"); $d=strtotime("+3 Months"); Try it Yourself » However, More Date ExamplesThe example below outputs the dates for the next six Saturdays: Example<?php while ($startdate < $enddate) { Try it Yourself » The example below outputs the number of days until 4th of July: Example<?php Try it Yourself » Complete PHP Date ReferenceFor a complete reference of all date functions, go to our complete PHP Date Reference. The reference contains a brief description, and examples of use, for each function! PHP ExercisesHow does PHP handle time and date?The PHP date() function is used to format a date and/or a time.. d - Represents the day of the month (01 to 31). m - Represents a month (01 to 12). Y - Represents a year (in four digits). l (lowercase 'L') - Represents the day of the week.. What is the date format in PHP?Localized Notations. How can get date in dd mm yyyy format in PHP?php //Set the default timezone to UTC. date_default_timezone_set('UTC'); echo "<strong>Display current date dd/mm/yyyy format </strong>". "<br />"; echo date("d/m/Y").
|