PHP Date and Time - PHP date() Function - How to Get the Current Date and Time in PHP



  • PHP Date and Time functions allow you to get the date and time from the server where your PHP script runs. You can then use the date and time in your code to format dates, calculate differences, or perform other operations.
php-date-time

The PHP Date() Function

  • The PHP date() function is used to format a date and/or a time.
  • It allows developers to format a date and time into a string that can be used in various ways, such as displaying it on a web page or storing it in a database.
  • It can also be used to compare dates and times, calculate time intervals, and convert a date and time into a different time zone.

Syntax

date(format,timestamp)
  • format - Required. Specifies the format of the timestamp
  • timestamp - Optional. Specifies a timestamp. Default is the current date and time

Get a Date

  • The PHP Get a Date function is a built-in function used to retrieve the current date and time from the server.
  • The function returns a formatted string that represents the current date and time.
  • The formatting of the returned string is based on the specified parameters and can be customized to suit the user's requirements.
    • 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

Sample Code

<?php
echo "Today is " . date("Y/m/d") . "<br>";
echo "Today is " . date("Y.m.d") . "<br>";
echo "Today is " . date("Y-m-d") . "<br>";
echo "Today is " . date("l");
?>

Output

Today is 2022/12/03
Today is 2022.12.03
Today is 2022-12-03
Today is Saturday

PHP Tip - Automatic Copyright Year

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

Sample Code

© 2008-<?php echo date("Y");?>

Output

© 2008-2020

Get a Time

  • The PHP get time function is used to get the current time in the Unix timestamp format, which is a numeric representation of the current time as the number of seconds elapsed since January 1, 1970 at 00:00:00 GMT.
    • H - 24-hour format of an hour (00 to 23)
    • 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)

Sample Code

<?php
echo "The time is " . date("h:i:sa");
?>

Output

08:10:13am 

Get Your Time Zone

  • The PHP get timezone function is used to retrieve the default time zone for the current web server. This function returns a string containing the name of the default time zone on success, or FALSE on failure.

Sample Code

<?php
date_default_timezone_set("Asia/calcutta");
echo "The time is " . date("h:i:sa");
?>

Output

The time is 12:59:37pm

Create a Date With mktime()

  • The PHP mktime() function is used to create a date.
  • It returns the Unix timestamp for a date.
  • The mktime() function takes in a series of parameters in a specific order to create a date.
  • The Unix timestamp returned by mktime() can be used to format the date and time.

Syntax

mktime(hour, minute, second, month, day, year)

Sample Code

<?php
$d=mktime(12, 14, 54, 6, 3, 2022);
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

Output

Created date is 2022-06-03 12:14:54pm 

Create a Date From a String With strtotime()

  • The PHP strtotime() function is used to convert a human readable date string into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).

Syntax

strtotime(time, now)

Sample Code

<?php
$d=strtotime("06:30pm june 3 2012");
echo "Created date is " . date("Y-m-d h:i:sa", $d);
?>

Output

Created date is 2012-06-03 06:30:00pm 

Related Searches to PHP Date and Time - PHP date() Function - How to Get the Current Date and Time in PHP