PHP Tutorials

PHP Date Functions

Date Functions in PHP

Date Functions in PHP

In this tutorial you will learn about some Date functions in PHP.

  1. Getting the Current Date
  2. Getting Number of days in a month
  3. Check the date format

For Date Formats refer: PHP Date Function Tips

1. Getting the Current Date

The PHP function 'date' is used to display the current date in the specified format as indicated by its arguments as shown in the above example. For more details on date format arguments, click PHP Date Function Tips.

Syntax

date('format options');

Example:

1.date('y m d'); Output: 05 04 23

2. date('Y M D'); Output: 2005 Apr Sat

3.date('Y m d h: s: m'); Output: 2005 04 23 02: 18: 04 (Date and Time)

2. Number of days in a month

The function 'cal_days_in_month' is used to return the number of days in a month. This function takes three arguments one for calendar name, month value and year value respectively.

Syntax

cal_days_in_month( CAL_GREGORIAN, $intMonth, $intYear);

Example:

cal_days_in_month ( CAL_GREGORIAN, 1, 2005); Output: 31

3. Check the date format

The function 'checkdate' is used to check whether the date passed as an argument is a valid date or not. The function returns a boolean value of '1' if the date is a valid date and 'Null' if the date is invalid.

Syntax

checkdate ($intMonth,$date, $intYear);

Example:

1. checkdate (1, 30,2005); Output: (1 means true)

2. checkdate (2, 30,2005); Output: (null means false)

That's it you have learnt about some of the most commonly used date functions in PHP.

Please like, +1, link to and share this SmartWebby resource if you found it helpful. Thanks!