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:

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:

Output:

-> 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:

Example3:

Output:

  • 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:

Output:

Example5:

Output:

Example6:

Output:

  • 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:

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:

Example

Output:

  • 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:

Example1:

Output:

Example2:

Output:

  • 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:

  • In the above example, the ceil function is used by which we calculate the time from the current date to the given date.

Pin It on Pinterest

Share This