How to create Date Time and Time Zone with PHP – We’ll try how to display the date and time using PHP. Because PHP already provides the date function, so we are very facilitated once. What if we try the PHP date function…
Creating a Date function with a pure PHP () is used to format the date and time.
PHP () Date function
The PHP () date format formats the timestamp to an easier-to-read date and time.
The syntax used
Date (format, timestamp)
The required format Parameter in the date () function specifies how to format the date (or time).
the commonly used characters for dates:
- D-is the day of the month (01 to 31)
- M-Represents the Moon (01 to 12)
- Y-is a year (in four digits)
- L (lowercase ‘ L ‘)-is a day of the week
Other characters, such as “/”, “. “, “or “- “can also be entered between characters to add additional formats.
The example below is today’s date format in three different ways:
<?php echo "Today is " . date("Y/m/d") . " "; echo "Today is " . date("Y.m.d") . " "; echo "Today is " . date("Y-m-d") . " "; echo "Today is " . date("l"); ?>
Create an auto copyright year
Use the date () function to update the copyright year on your site automatically:
<?php © 2010- echo date("Y");?>
Making a dull time with PHP
some commonly used characters for times:
- H-Format 12 hours an hour with leading zeros (01 to 12)
- I-minutes with leading zeros (00 to 59)
- S-seconds with zero numbers (00 to 59)
- A-Lowercase Ante meridiem and Post meridiem (AM or PM)
The example below shows the current time in the specified format:
<?php echo "The time is ". Date ( "h:i: SA ");?>
Creating time zones with PHP
If when you return from the code is not the right time, it may be because the server is in another country or set up for a different time zone.
if you need time to match a specific location accurately, you can set a time zone to use.
example to below sets the time zone to “America/New_York “, and then displays the current time in specified format:
<?php date_default_timezone_set("America/New_York"); echo "The time is " . date("h:i:sa"); ?>
That’s How to create Date Time and Time Zone with PHP easiest by AlgoritmComputer.com, share this article if useful.