How to create Date Time and Time Zone with PHP

How to create Date Time and Time Zone with PHP
How to create Date Time and Time Zone with PHP

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:

  1. D-is the day of the month (01 to 31)
  2. M-Represents the Moon (01 to 12)
  3. Y-is a year (in four digits)
  4. 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:

Example :

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:

Example :

<?php © 2010- echo date("Y");?>

Making a dull time with PHP

some commonly used characters for times:

  1. H-Format 12 hours an hour with leading zeros (01 to 12)
  2. I-minutes with leading zeros (00 to 59)
  3. S-seconds with zero numbers (00 to 59)
  4. A-Lowercase Ante meridiem and Post meridiem (AM or PM)

The example below shows the current time in the specified format:

Example :

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

Example :

echo "The time is " . date("h:i:sa");

That’s How to create Date Time and Time Zone with PHP easiest by, share this article if useful.

Related posts

Leave a Reply

Your email address will not be published. Required fields are marked *