1) PHP Date and time

Setting a default timezone

Before starting to work to date and time function you must set default timezone for your country to get the correct result. You can check all supported time zones here.
First, check what is your default timezone. To check, Create file config.php

Save and run the file on your local system. Search for date >> Default Timezone
php date and time

You can change the timezone on php.ini file
php.ini
Save the file restart your server then check config. For a remote server, if you don’t have access to php.ini file you can change timezone with a .htaccess file

PHP Basic Date and Time function

Display current date or time:

PHP date function displays the date and time using server default timezone

The function takes two arguments

    • 1st parameter is required is a string in the format of a date.
    • 2nd parameter is optional parameter sets at a time. If we exclude the second argument it defaults to current time.

The date function is quite simple to remember but there are a lot of formatting options available and you can always have a look at the official document for this as it is very well explained there and
LIST OF DATA TIME FUNCTION

Example

Note: Output will the day for you. This is Tuesday for me.

Example 2:

Output: Today is Tuesday, May 1, 2018

      • F – full name of the month.
      • j-date without leading zero.
      • Y- year 4 digit.

Output: Today is Tuesday, May 1, 2018
Time is 9:06:48 am

      • Lowercase g > gives hour in 12-hour format.
      • Lowercase i > give minutes.
      • s: represent second
      • a: represents am or pm in lowercase

We can also use plain text within date function but make sure to escape the text character

Example

Output: Today is 9:11:38 am on Tuesday, May 1, 2018
Date functions can also be used with predefined constants and you can find this in official documentation of PHP
DATE FUNCTION CONSTANTS

Example:

DATE_COOKIE: Will format the date for the cookie

Getting timestamp from a text:

strtotime function takes string as argument and convert it to Unix timestamp. In the example above the value returned by strtotime is stored as timestamp and is passed as argument to date function. When you do that it displays date stored in timestamp not the current date.
strtotime can be used to pass text values also

For example:

Get the relative date

We can also add or subtract the number of days or week
Ex: Find 3 weeks after the new year 2018

Get the full list from HERE