PHP Date and Time

DATE:

The PHP date() function formats a date or time.

The timestamp is used to format a more readable date and time into a PHP date () function.

php date and time

Syntax:

date(format,timestamp)

Simple Date:

Characters that are mainly used for dates:

d: The “d” is used to get the day of the month (from 1 to 31).

m: The “m” is used to get the month (From (1) to (12)).

y: The “y” is used to get the year ( in four digits).

l: The lowercase (l) is used to represent the day of the week.

Other special characters, like “/”, “.”, or “-“ can also be used between the characters to add additional formatting.

Example1:

"; 
 echo "Today is” . date("m/d/Y") . "
"; echo "Today is” . date("Y/m/d") . "
"; ?>

Output:

Today is 22/06/2019
Today is 06/22/2019
Today is 2019/06/22

-> In the above example, we get to know that we can write a date into a different format with the help of characters.

Example2:

"; 
 ?> 

Output:

Today`s Date Is 2019.06.22

Example3:

"; 
 echo "Today " . date("d") . "
"; echo  date("M") . "
"; echo " Year " . date("Y") . "
"; ?>

Output:

Hello Saturday
Today 22
Jun
Year 2019
  • In the above example, “M” is used to define the month name.

With the help of the date() function, we can update copyright year on our website.

Example4:

© 2018-|All Rights Reserved|www.tutorialandexample.com|

Output:

  © 2018-2019|All Rights Reserved|www.tutorialandexample.com| 

Example5:

 

Output:

Date is valid

Example6:

";
 print_r($sun);
 echo date("H:i:s",$sun);
 ?> 

Output:

Array
 (
    [sunrise] => 1561247655
    [sunset] => 1561297943
    [transit] => 1561272799
    [civil_twilight_begin] => 1561246042
    [civil_twilight_end] => 1561299557
    [nautical_twilight_begin] => 1561244083
    [nautical_twilight_end] => 1561301515
    [astronomical_twilight_begin] => 1561241999
    [astronomical_twilight_end] => 1561303599
)
01:54:15 
  • In the above example, with the help of our current location longitude and latitude, we can easily find the sunrise, sunset, current time, etc.

TIME:

Characters that are mainly used for time:

H: The H character is used for 24hour format (00 to 23).

h: The lowercase “h” is used for 12hour format of an hour(01 to 12).

i: The lowercase “i” is used for minutes with leading zero(00 to 59).

s: The lowercase “s” is used for seconds with leading zero (00 to 59).

a: The “a” is used for ANTE MERIDIEM (a.m.) or POST MERIDIEM (p.m.).

Time Zone:

We can set a timezone to use; we need the time correct according to a specific location.

If the time we get from the code is not the right time, it is because our server is in another country or it set up on a different timezone.

Example:

 

Output:

The time is 04:20:19.pm.

PHP mktime() Function:

The timestamp parameter in the date() function defines a timestamp.

The mktime() function returns the Unix timestamp for a date. It contains the number of seconds between the Unix Epoch (June 23, 2019, 00:00:00 GMT).

Syntax:

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

Example

 

Output:

Current Date and Time is 2019-06-23 12:30:10pm
  • In the above example, the mktime() function creates a date and time from several of parameters.

PHP strtotime():

The PHP strtotime() function is used to convert a human-readable string into Unix Time.

Syntax:

strtotime(time,now)

Example1:

" . "
"; $d=strtotime("next Saturday"); echo date("d-m-Y h:i:sa", $d) . "
" . "
"; $d=strtotime("+3 Months"); echo date("d-m-Y h:i:sa", $d) . "
" . "
"; $d=strtotime("+6 Years"); echo date("d-m-Y h:i:sa", $d) . "
" . "
"; ?>

Output:

24-06-2019 12:00:00am
29-06-2019 12:00:00am
23-09-2019 10:37:14am
23-06-2025 10:37:14am

Example2:

"; 
 $startdate = strtotime("+2 days", $startdate); 
 } 
 ?> 

Output:

Jun 28
Jun 30
Jul 02
Jul 04
Jul 06
Jul 08
Jul 10
Jul 12
Jul 14
Jul 16
Jul 18
Jul 20
Jul 22
Jul 24
Jul 26
Jul 28
Jul 30
Aug 01
Aug 03
Aug 05
Aug 07
  • The second and third line of the above code is telling us about the starting date or day(here we have selected the Friday) which means the execution will start from Friday, and it lasts to 6weeks, and it will return Month and date to the user and it will also return weekdays with the difference of 2.

Example3:

 

Output:

There are 53 days remain left for Independence Day.
  • In the above example, the ceil function is used by which we calculate the time from the current date to the given date.

Related Topics

PHP Forms

PHP form is used to take input from users, by using superglobals$_REQUEST, $_GET and $_POSTmethod. There are various request methods in PHP Get method Post method Put method Patch method Delete...

5 minutes read.

PHP array_multisort() Function

PHP array_multisort () Function The array_multisort() function in PHP sorts multiple or multi-dimensional arrays at once. Syntax array_multisort ( array &$array1 [, mixed $array1_sort_order [, mixed $array1_sort_flags [, mixed $... ]]] ) Parameter array1(required)- This parameter specified the input array to sort. array1_sort_order(optional)- This parameter specifies the order (ascending or descending)...

1 minute read.

PHP is_null() Function

The is_null() function in PHP is used to  find whether the specified variable is a null or not. It returns a Boolean value true if the parameter var is null else it returns...

1 minute read.

PHP is_int() Function

The PHP is_int() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it...

1 minute read.

PHP For Loops

PHP for loop is used when the specified condition is predefined. Syntax: for (initialization; condition; increment/decrement ) { code to be executed; } Example <!DOCTYPE html> <html> <head> <title>PHP Tutorial</title> </head> <body> <?php echo " Print counting using for loop"."<br>"; for($i=1;$i<=5;$i++){ echo $i."<br>"; } ?> </body> </html> Output Print counting using...

1 minute read.

PHP htmlspecialchars_decode() Function

PHP htmlspecialchars_decode() Function The htmlspecialchars_decode() function in PHP convert some predefined HTML entities to characters. The some predefined HTML entities that will be decoded are as follows: &amp; OR & (ampersand)&quot; OR " (double quote)&#039;...

2 minutes read.

PHP rsort() Function

PHP rsort() Function The rsort () function in PHP is used to sort an array in reverse order i.e., highest to lowest. Syntax rsort ( array &$array [, int $sort_flags = SORT_REGULAR ] ) Parameter array(required)- This parameter represents the input array. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP array_intersect() function

The array_intersect () function in PHP is used to compute the intersection of arrays. It returns an array containing all the entries from array1 that are present in all the other arrays. Syntax array_intersect ( array $array1 , array $array2 [, array $... ]) Parameter array1(required)- This parameter...

1 minute read.

Python Set pop() method

Python Set pop() method The set.pop() method in Python removes an arbitrary element (random item) from the set and returns the element removed. Syntax set.pop()  Parameter NA Return This method returns an arbitrary (random) element from the set. Example 1 #...

1 minute read.

PHP strcmp() Function

PHP strcmp() Function The strcmp() function in PHP compares two strings. It is a binary-safe case-sensitive string function. This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each...

1 minute read.

PHP list() Function

PHP list() Function The list() function in PHP sorts an array by key while maintaining the keys. Syntax list ( mixed $var1 [, mixed $... ] ) Parameter var1(required)- This parameter accepts variables separated by spaces. …(optional)- This parameter accepts a list of variables separated...

1 minute read.

PHP str_repeat() Function

PHP str_repeat() Function The str_repeat() function in PHP repeats a string a specified number of times. Syntax str_repeat ( string $input , int $multiplier ) Parameter string(required)- This parameter signifies the string to be repeated. multiplier(required)- This parameter represents the number of times the string will be...

1 minute read.

PHP levenshtein() Function

PHP levenshtein() Function The levenshtein() function in PHP calculates the Levenshtein distance between two strings. Syntax levenshtein ( string $str1 , string $str2 , int $cost_ins , int $cost_rep , int $cost_del )  Parameter str1(required)- This parameter signifies one of the strings being evaluated for Levenshtein distance. str2(required)- This parameter signifies one of the strings being evaluated...

1 minute read.

PHP str_pad() Function

PHP str_pad() Function The str_pad() function in PHP pads a string to the given length with another string. Syntax str_pad ( string $input , int $pad_length [, string $pad_string  [, int $pad_type ]] ) Parameter input(required)- This parameter signifies the input string. pad_length(required)- This parameter specifies the length of the new string. If this value...

2 minutes read.

PHP lcfirst() Function

PHP lcfirst() Function The lcfirst() function in PHP converts the first character of a string to lowercase if that character is alphabetic. Syntax lcfirst ( string $str ) Parameter str(required)- This parameter signifies the input string. Return This function returns a string with the...

1 minute read.

PHP strrchr() Function

PHP strrchr() Function The strrchr() function in PHP finds the last occurrence of a character in the specified string and returns the portion of haystack, which starts at the last occurrence of the needle and goes until the end of haystack....

1 minute read.

PHP is_real() Function

The PHP is_real () function in PHP is used to find whether the type of the specified variable is a float or not. It returns a Boolean value true if the parameter...

1 minute read.

PHP foreach Loop

PHP foreach Loop with Example PHP foreach loop is used to loop through the associative arrays. Syntax: foreach($array as $key => $value) { //Statement } $array = associative array. $key = array key. &value =...

2 minutes read.

PHP is_integer() Function

The PHP is_integer() function in PHP is used to  find whether a variable is an integer or not. It returns a Boolean value true if the parameter var is integer else it returns...

1 minute read.

PHP str_replace() Function

PHP str_replace() Function The str_replace() function in PHP replaces all occurrences of the search string with the specified replacement string. This function is case-sensitive. Syntax str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) Parameter search(required)- This parameter signifies the value to search. replace(required)-...

1 minute read.