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 money_format() Function

PHP money_format() Function The money_format() function in PHP formats a number as a currency string and returns a formatted version of number. Syntax money_format ( string $format , float $number )  Parameter format(required)- This parameter specifies the string to be formatted and how to format the...

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.

PHP array_pop() Function

PHP array_pop() Function The array_pop() function in PHP pops or removes the last element from the array shortening the array by one element. Syntax array_pop ( array &$array ) Parameter array(required)- This parameter represents the input array to get the value...

1 minute read.

PHP asort() function

PHP asort() function The asort() function in PHP is used to sort an array while maintaining the index association. Syntax asort ( array &$array [, int $sort_flags = SORT_REGULAR ] )  array(required)- This parameter represents the input array to sort. sort_flags (optional)- This parameter is used to...

1 minute read.

PHP empty() Function

PHP empty() Function The empty() function in PHP determines whether the specified variable is empty or not. The specified variable is considered empty if it does not exist or if its value is equal to FALSE. This function...

1 minute read.

PHP array_slice() Function

PHP array_slice() Function The array_slice() function in PHP is used to extract a slice of the array. It returns the sequence of elements from the array as specified by the offset and length parameters. Syntax array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = FALSE ]] ) Parameter array(required)- This array represents the...

2 minutes read.

PHP stripos() Function

PHP stripos() Function The stripos() function in PHP finds the position of the first occurrence of a substring in the specified string. It is a case-insensitive a binary-safe function. The related functions are as follows: strripos() Functionstrpos() Functionstrrpos() Function Syntax stripos ( string $haystack , mixed $needle [, int $offset...

1 minute read.

PHP Operator

PHP Operator is used to perform operations on variable and values. Following are the types of Operator: PHP Arithmetic Operator PHP Arithmetic operator is used for arithmetical operations, such as addition, subtraction, multiplication...

2 minutes read.

PHP MySQL Database Tutorial

MySQL is the most popular database system that is used within PHP. Let's first understand about MySQL. What is MySQL? MySQL is open-source Relational Database Management System (RDMS). The name of MySQL is...

13 minutes read.

PHP Comment

PHP commentis a line of code that is not executed. It is often used by developer to understand the code. PHP support single and multiple comments. In other language like C,...

1 minute read.

PHP String get_html_translation_table() Function

The get_html_translation_table() function in PHP is used to return the translation table used by the htmlentities() and htmlspecialchars() functions. Syntax get_html_translation_table ([ int $table = HTML_SPECIALCHARS [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = "UTF-8" ]]] ) Parameter table (optional)- This parameter specifies which translation table to return. It...

2 minutes read.

Python if-else

Python if-else The else statement is associated with if statement. An else statement executes if the conditional expression in if the statement becomes false or a Zero value. If the conditional expression...

2 minutes read.

PHP Data Types

PHP data type is used to hold the various types of data or value.  There are three types of data types in PHP. It is a loosely data typed language, so...

2 minutes read.

PHP array_splice() Function

PHP array_splice() Function The array_splice() function in PHP is used to remove a portion of the array and replaces them with the elements of the replacement array. Syntax array_splice (array &$input, int $offset [, int $length = count($input) [, mixed $replacement = array() ]] ) Parameter input(required)- This array represents the...

2 minutes read.

PHP substr_compare() Function

PHP substr_compare() Function The substr_compare() function in PHP compares two strings from a specified start position. It is a binary-safe and optionally case-sensitive function. Syntax substr_compare ( string $main_str , string $str , int $offset [, int $length [, bool$case_insensitivity]] )  Parameter main_str(required)- This parameter specifies the first string to compare. str(required)- This parameter...

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 pos() Function

PHP pos() Function The pos() function in PHP returns the current element in an array which is initialized to the first element of the array. This function is an alias of current(). Syntax pos ( array...

1 minute read.

PHP array_rand() Function

PHP array_rand() Function The array_rand() function in PHP picks one or more random keys out of an array. Syntax array_rand ( array $array [, int $num = 1 ] ) Parameter array(required)- This parameter represents the input array. num(optional)- This parameter specifies how many entries should be picked....

1 minute read.

PHP array_merge_recursive() Function

PHP array_merge_recursive() Function The array_merge_recursive() function  in PHP merges the elements of one or arrays together and returns the resulting array. Syntax array_merge_recursive (array $array  , [ array $... ] ) Parameter array(required)- This parameter specifies the input array ….(optional)- It represents more arrays to...

1 minute read.

PHP array_fill() function

PHP array_fill() function The array_fill() function in PHP fills an user-defined array with num entries of the value that of specified values parameter, where the keys start at the start_index parameter. Syntax array_fill ( int $start_index , int $num , mixed $value ) Parameter start_index(required)- This parameter represents the first index of the...

1 minute read.