Belajar Tidak Identik dengan Buku

The PHP Date() Function

The PHP date() function formats a timestamp to a more readable date and time.
Syntax : date(format,timestamp).

Parameter Description
format Required. Specifies the format of the timestamp
timestamp Optional. Specifies a timestamp. Default is the current date and time

Get a Simple Date

The required format parameter of the date() function specifies how to format the date (or time).
Here are some characters that are commonly used for dates:
  • d – Represents the day of the month (01 to 31)
  • m – Represents a month (01 to 12)
  • Y – Represents a year (in four digits)
  • 1 – Represents the day of the week
Other characters, like”/”, “.”, or “-” can also be inserted between the characters to add additional formatting.
The example below formats today’s date in three different ways:
Example :
<?php
echo “Today is ” . date(“Y/m/d”) . “<br>”;
echo “Today is ” . date(“Y.m.d”) . “<br>”;
echo “Today is ” . date(“Y-m-d”) . “<br>”;
echo “Today is ” . date(“l”);
?>

Output :

Today is 2014/05/11
Today is 2014.05.11
Today is 2014-05-11
Today is Sunday



Get a Simple Time

Here are some characters that is commonly used for times:

  • h – 12-hour format of an hour with leading zeros (01 to 12)
  • i – Minutes with leading zeros (00 to 59)
  • s – Seconds with leading zeros (00 to 59)
  • a – Lowercase Ante meridiem and Post meridiem (am or pm)


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

Example :

<?php
echo “The time is ” . date(“h:i:sa”);
?>

Output :

The time is 09:43:52pm

Note that the PHP date() function will return the current date/time of the server!


Get Your Time Zone

If the time you got back from the code is not the right time, it’s probably because your server is in another country or set up for a different timezone.
So, if you need the time to be correct according to a specific location, you can set a timezone to use.
The example below sets the timezone to “America/New_York”, then outputs the current time in the specified format:

Example :

<?php
date_default_timezone_set(“America/New_York”);
echo “The time is ” . date(“h:i:sa”);
?>

Output :
The time is 09:46:13pm

Related Posts

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Happy Paperless located at Banjarnegara , Jawa Tengah, Indonesia . Reviewed by 28 customers rated: 1 / 5



Rumus-Excel.Com
Kunjungi rumus-excel.com untuk belajar excel. Anda juga dapat menyumbangkan artikelnya ke kami untuk kita publish