Pakhermawan The PHP Date() Function Minggu, 11 Mei 2014 The PHP date() function formats a timestamp to a more readable date and time. Syntax :  date( format , timestamp ). Parameter Description fo... 5

The PHP Date() Function

0
The PHP date() function formats a timestamp to a more readable date and time.
Syntax : date(format,timestamp).
ParameterDescription
formatRequired. Specifies the format of the timestamp
timestampOptional. 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

Tidak ada komentar:

Posting Komentar


Copyright © Toturial 4 U

Template By: Fauzi Blog Sponsored By: 34DL - Free For Download