PHP Date and Time
Using the PHP date and time functions, we can get the current date, we can set a date, and we can do a different date and time manipulations and essentially display it in a nice format. So let's start off with our date function.
<html> <head> <title>My First PHP App</titile> </head> <body> echo date('m'); </body> </html>
date('m') function is going to show us the current month (11). You can also print out which day it is and which year it is. You can do everything at the same time.
This will print out the current month, date, and year
(11-28-2020). There are different ways you can format the current date. You can give the capital
F, which will provide us with the string representation of the current month
We can also say lowercase
l, which will give us the current day of the week
If you want to check more about these date functions, I would recommend you check out the this PHP documentation.
We can also display the current time.
echo date('h:i:sa); ?>
h, we are showing the hour. With the
i, we are showing the minute, and with the
s, we are showing the seconds. The
a is going to give us a representation of whether this is an a.m. or p.m.
You will notice that its not showing your time zone. It will be showing the UTC time zone by default which is the universal time zone. In most cases this is what PHP uses as default. But we can change that by changing the current time zone:
'America/Los_Angeles'); echo date('h:i:sa); ?>date_default_timezone_set(
This will show us the America/Los Angeles time zone.