PHP Basics
Functions in PHP
Working with Forms
Working with Files
Working with Databases
Advanced PHP Techniques
PHP provides a rich set of functions to handle dates and times. These functions are part of the ‘DateTime class and related procedural functions. Here’s a deep dive into some of the key date and time functions in PHP:

1. date()

The ‘date()‘ function formats a local date and time and returns it as a string.

Syntax:

				
					string date ( string $format [, int $timestamp = time() ] )

				
			

Parameters:

  • $format: A string that defines the output format. Some common format characters include:
    • d – Day of the month (01 to 31)
    • m – Numeric representation of a month (01 to 12)
    • Y – Four-digit year (e.g., 2024)
    • H – 24-hour format of an hour (00 to 23)
    • i – Minutes with leading zeros (00 to 59)
    • s – Seconds with leading zeros (00 to 59)
  • $timestamp: (optional) An integer Unix timestamp. Default is the current time if omitted.

Example:

				
					echo date('Y-m-d H:i:s'); // Outputs current date and time in 'YYYY-MM-DD HH:MM:SS' format
echo date('Y-m-d', strtotime('2024-07-03')); // Outputs '2024-07-03'

				
			

2. time()

The ‘time()‘ function returns the current Unix timestamp, which is the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).

Syntax:

				
					int time ( void )

				
			

Example:

				
					echo time(); // Outputs the current Unix timestamp

				
			

3. mktime()

The ‘mktime()‘ function returns the Unix timestamp for a date.

Syntax:

				
					int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

				
			

Parameters:

  • $hour, $minute, $second, $month, $day, $year: These parameters are optional and default to the current time if omitted.
  • $is_dst: This parameter can be used to set whether the time is in daylight saving time.

Example:

				
					echo mktime(0, 0, 0, 7, 3, 2024); // Outputs the Unix timestamp for July 3, 2024

				
			

4. strtotime()

The ‘strtotime()‘ function parses an English textual datetime description into a Unix timestamp.

Syntax:

				
					int strtotime ( string $time [, int $now = time() ] )

				
			

Parameters:

  • $time: A date/time string. Some examples include “now”, “10 September 2000”, “+1 day”, “+1 week”, “next Thursday”, “+1 week 2 days 4 hours 2 seconds”.
  • $now: (optional) The timestamp which is used as a base for the calculation of relative dates.

Example:

				
					echo strtotime('next Monday'); // Outputs the Unix timestamp for the next Monday

				
			

5. DateTime Class

The ‘DateTime‘ class provides more object-oriented ways to handle date and time.

Creating a DateTime Object:

				
					$date = new DateTime('2024-07-03');
echo $date->format('Y-m-d H:i:s'); // Outputs '2024-07-03 00:00:00'

				
			

Modifying a DateTime Object:

				
					$date = new DateTime('2024-07-03');
$date->modify('+1 day');
echo $date->format('Y-m-d'); // Outputs '2024-07-04'

				
			

Difference Between Dates:

				
					$date1 = new DateTime('2024-07-03');
$date2 = new DateTime('2024-08-03');
$interval = $date1->diff($date2);
echo $interval->format('%R%a days'); // Outputs '+31 days'

				
			

Adding/Subtracting Time Intervals:

				
					$date = new DateTime('2024-07-03');
$date->add(new DateInterval('P10D')); // Adds 10 days
echo $date->format('Y-m-d'); // Outputs '2024-07-13'

$date->sub(new DateInterval('P10D')); // Subtracts 10 days
echo $date->format('Y-m-d'); // Outputs '2024-07-03'

				
			

6. DateTimeZone Class

The ‘DateTimeZone‘ class represents time zones.

Example:

				
					$timezone = new DateTimeZone('America/New_York');
$date = new DateTime('now', $timezone);
echo $date->format('Y-m-d H:i:s'); // Outputs the current date and time in New York

				
			

Common Date and Time Formats

  • Y-m-d‘: ‘2024-07-03
  • m/d/Y‘: ‘07/03/2024
  • d-m-Y' 'H:i:s‘: ‘03-07-2024' '14:55:00
  • l, F j, Y‘: ‘Wednesday, July 3, 2024

PHP’s date and time functions provide a comprehensive set of tools to work with dates and times in various formats and contexts. Understanding and effectively using these functions can greatly enhance your ability to manage and manipulate date and time data in your applications.

Scroll to Top