In PHP there is a function available for get date and time that is date(format, timestamp) function.
Let’s see the parameter format & timestamp in details –
Parameter | Description |
---|---|
format | Required. Specifies the format of the outputted date string. The following characters can be used:
|
timestamp | Optional. Specifies an integer Unix timestamp. Default is the current local time (time()) |
For more details about date parameter you can see here.
Let’s see the example –
<?php
echo date('d-m-Y');
Output :(It’ll print today’s date)
10-06-2020
Now let’s see how to get yesterday date in PHP –
<?php
echo date('d-m-Y', strtotime(' -1 day'));
Output:
09-06-2020
Similarly, we can get 1 week, 1 month, 1 year or n numbers of days back or ago date by simple use of one line function. For getting future date just delete -(minus) from above function.It’ll give you future date.
We can also set default time zone.
<?php
date_default_timezone_set("Asia/Kolkata");
echo date_default_timezone_get();
?>
Output:
Asia/Kolkata
Click here for get list of all available time zone.