PHP Date and Time Function

We’ll talk about PHP Date and Time Function today. Prior to these, we also covered PHP Exception And Error Handling TutorialPHP MySQLi Functions: mysqli_query | mysqli_connect | mysqli_fetch_array, Forms in PHPPHP SessionPHP Operators, and Mail Function In PHP. If you don’t know about these, then act quickly!

PHP date() Function

The built-in date function in PHP makes working with date data types simpler. To format a date or time into a human-readable format, use the PHP date function. It can be used to show the publication date of an article. record a data’s most recent update in a database. The built-in date function in PHP makes working with date data types simpler. To format a date or time into a human-readable format, use the PHP date and time function. It can be used to show the publication date of an article. record a data’s most recent update in a database.

PHP Date Syntax & Example

Basic Syntax:

<?php
date(format,[timestamp]);
?>

Let’s take an example:

<?php
echo date("Y");
?>
OUTPUT:
2022

What is a PHP TimeStamp?

The difference in seconds between the current time and the value as of January 1st, 1970, 00:00:00 Greenwich Mean Time, is known as a timestamp in PHP (GMT).

The default time zone affects the value that the time function returns.

The php.ini file specifies the default time zone.

Additionally, it can be programmed using the date default_timezone_set function.

The timestamp is shown in the code below.

<?php
echo time();
?>

PHP Time Parameters

ParameterDescriptionExample
“r”returns the complete time and date.<?php
echo date(“r”);
?>  
“a”,”A”returns whether it is morning or evening, AM or PM, respectively.<?php
echo date(“a”);
echo date(“A”);
?>  
“g”,”G”returns [1 to 12], [0 to 23], respectively, the hour without leading zeros.<?php
echo date(“g”);
echo date(“G”);
?>  
“h”,”H”  returns the hour with the appropriate leading zeros ([01 to 12], [00 to 23]).<?php
echo date(“h”);
echo date(“H”);
?>  
“i”,”s”  returns the minutes and seconds (from 00 to 59) with leading zeroes.<?php
echo date(“i”);
echo date(“s”);
?>  

Day parameters:

ParameterDescriptionExample
“d”returns the day of the month, from 1 to 31, with leading zeros.<?php
echo date(“d”);
?>  
“j”returns the day of the month [1 to 31] without the leading zeros.<?php
echo date(“j”);
?>  
“D”  returns [Sub to Sat] the first 3 letters of the day’s name.<?phpecho date(“D”);
?>  
“l”  gives the name of the week’s day (from Sunday to Saturday).<?php
echo date(“l”);
?>  
“w”  Returns the day of the week (0 to 6) without any leading zeros. Sunday is represented by 0 and continues through Saturday, which is represented by 6. (6).<?php
echo date(“w”);
?> 
“z”  returns the day of the year (0 through 365) without any leading spaces.<?php
echo date(“z”);
?> 

Month Parameters

ParameterDescriptionExample
“m”returns the month’s numeric value, which ranges from 01 to 12.<?php
echo date(“m”);
?>  
“n”returns the month number [01 to 12] without the leading zeros.<?php
echo date(“n”);
?> 
“M”  returns [Jan to Dec] the first 3 letters of the month’s name.<?php
echo date(“M”);
?>  
“F”  gives the name of the month (from January to December).<?php
echo date(“F”);
?>   
“t”  returns the [28 to 31] days that make up a month.<?php
echo date(“t”);
?> 

Year Parameters

ParameterDescriptionExample
“L”If a leap year is present, this function returns 1, otherwise, it returns 0.<?php
echo date(“L”);
?>  
“Y”four-digit year format is returned.<?php
echo date(“Y”);
?> 
“y”  returns a two-digit year in the format (00 to 99).<?php
echo date(“y”);
?>  

You Might Like

Leave a Reply

Your email address will not be published.

Sorry
Sorry
Sorry
Sorry
Sorry
Sorry
Sorry