PHP Date & Time

In PHP, the date and time functions are used to work with dates and times. Some of the most commonly used functions are:

1) date(): The  date()  function is used to format a date and time.

The syntax of the function is as follows:

date('Y-m-d');

//Y = Current YEAR 
//m = Current Month
//d = Current Date

 

2) time(): The time() function is used to get the current Unix timestamp. The Unix timestamp is the number of seconds since January 1, 1970 00:00:00 UTC.

For example, to get the current Unix timestamp, you would use the following code:

echo time();

 

3) strtotime(): The strtotime() function is used to convert a date string to a Unix timestamp.

The syntax of the function is as follows:

echo strtotime("2023-02-19");

 

DIfferent Type of Date Format:

<?php

// set the default timezone to use.
date_default_timezone_set('Asia/Kolkata');

// 2023-02-20 01:11:09 (the MySQL DATETIME format)
echo date("Y-m-d H:i:s").'<br>'; 

// 02.20.23
echo date("m.d.y").'<br>'; 

// 01:11:09
echo date("H:i:s").'<br>'; 

// Prints something like: Monday
echo date("l").'<br>';

// Prints something like: Monday 20th of February 2023 01:06:39 AM
echo date('l jS \of F Y h:i:s A').'<br>';

// prints something like: Monday the 20th
echo date('l \t\h\e jS').'<br>';

// March 10, 2001, 5:16 pm
echo date("F j, Y, g:i a").'<br>';   

// 20, 2, 2023
echo date("j, n, Y").'<br>';

// it is the 20th day.
echo date('\i\t \i\s \t\h\e jS \d\a\y.').'<br>'; 

// 01:02:09 m is month
echo date('H:m:s \m \i\s\ \m\o\n\t\h').'<br>';

?>