Close

PHP Five Date and Time

PHP 5 Date and Time

The PHP date() function is used to format a date and/or a time.

The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.

Syntax

A timestamp is a sequence of characters, denoting the date and/or time at which a certain event occurred.

Get a Ordinary Date

The 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:

  • 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

Other characters, like",/",, ",.",, or ",-", can also be inserted inbetween the characters to add extra formatting.

The example below formats today’s date in three different ways:

Example

PHP Peak – Automatic Copyright Year

Use the date() function to automatically update the copyright year on your website:

Example

Get a Ordinary Time

Here are some characters that are commonly used for times:

  • h – 12-hour format of an hour with leading zeros (01 to 12)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds with leading zeros (00 to 59)
  • a – Lowercase Ante meridiem and Post meridiem (am or pm)

The example below outputs the current time in the specified format:

Example

Note that the PHP date() function will comeback the current date/time of the server!

Get Your Time Zone

If the time you got back from the code is not the right time, it’s most likely 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 a timezone to use.

The example below sets the timezone to ",America/New_York",, then outputs the current time in the specified format:

Example

Create a Date With PHP mktime()

The optional timestamp parameter in the date() function specifies a timestamp. If you do not specify a timestamp, the current date and time will be used (as shown in the examples above).

The mktime() function comes back the Unix timestamp for a date. The Unix timestamp contains the number of seconds inbetween the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

Syntax

The example below creates a date and time from a number of parameters in the mktime() function:

Example

$d=mktime(11, 14, 54, 8, 12, 2014),

Create a Date From a String With PHP strtotime()

The PHP strtotime() function is used to convert a human readable string to a Unix time.

Syntax

The example below creates a date and time from the strtotime() function:

Example

$d=strtotime(",Ten:30pm April 15 2014",),

PHP is fairly clever about converting a string to a date, so you can put in various values:

Example

However, strtotime() is not flawless, so recall to check the strings you put in there.

More Date Examples

The example below outputs the dates for the next six Saturdays:

Example

$enddate = strtotime(",+6 weeks",, $startdate),

$startdate = strtotime(",+1 week",, $startdate),

The example below outputs the number of days until 4th of July:

Example

echo ",There are ", . $d2 .", days until 4th of July.",,

Accomplish PHP Date Reference

For a finish reference of all date functions, go to our accomplish PHP Date Reference.

The reference contains a brief description, and examples of use, for each function!

COLOR PICKER

HOW TO

SHARE

CERTIFICATES

HTML, CSS, JavaScript, PHP, jQuery, Bootstrap and XML.

Related movie: Genevieve Zawada, CEO of Elect Club Reviews the iDate Matchmaking Convention Track London 2016


Leave a Reply

Your email address will not be published. Required fields are marked *