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
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");
?>
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