Write a php program to display current date time and day
View Discussion Show
Improve Article Save Article View Discussion Improve Article Save Article The purpose of the article is to get the current Date and Time in PHP. It is performed by using a simple in-built PHP function date(). The Date is an inbuilt function used to format the timestamp. The computer stores date and time in UNIX timestamp. This time is measured as a number of seconds since Jan 1, 1970. As this is difficult for humans to read, PHP converts timestamps to format in such a way that it is readable and more understandable to humans. Syntax: date('d-m-y h:i:s'); Parameters : The date() have different parameters. Each parameter represents some meaningful unit.
Example 1: PHP
Output: Current date and time is :03-01-21 04:49:52 Example 2: The following demonstrates other date() functions format which can be used by the developer as per the need. PHP
Output: The PHP 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: Exampleecho "Today is " . date("Y/m/d") . " 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: Exampledate_default_timezone_set("America/New_York"); 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$d=mktime(11, 14, 54, 8, 12, 2014); Try it Yourself » Create a Date From a String With strtotime()The PHP SyntaxThe example below creates a date and time from the Example$d=strtotime("10:30pm April 15 2014"); Try it Yourself » PHP is quite clever about converting a string to a date, so you can put in various values: Example$d=strtotime("tomorrow"); $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$startdate = strtotime("Saturday"); while ($startdate < $enddate) { Try it Yourself » The example below outputs the number of days until 4th of July: Example$d1=strtotime("July 04"); 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 can get current date and day in PHP?Answer: Use the PHP date() Function
You can simply use the PHP date() function to get the current data and time in various format, for example, date('d-m-y h:i:s') , date('d/m/y H:i:s') , and so on.
How can I insert current time in PHP?Examples: DATE: YYYY-MM-DD Example: 2005-12-26 DATETIME: YYYY-MM-DD HH:MI:SS Example: 2005-12-26 23:50:30 TIMESTAMP: YYYY-MM-DD HH:MI:SS Example: 2005-12-26 23:50:30 YEAR: YYYY or YY.. MySQL query to create DataBase: ... . Example 1: PHP program to Create database and table. ... . Example 2: PHP program to insert date into the table.. How can I get current date in PHP string?Doing this : $date = date("D M d, Y G:i"); You'll have the current date in the $date variable, as a string -- no need for any additional operation.
How can I get current date and time in MySQL and PHP?MySQL SYSDATE() returns the current date and time in YYYY-MM-DD HH:MM:SS or YYYYMMDDHHMMSS. uuuuuu format depending on the context of the function. MySQL TIME_FORMAT() converts a time in a formatted string using the format specifiers. MySQL TIME_TO_SEC() converts a time value in to seconds.
|