You are on page 1of 37

FCAIT

0301501 Website BCA


Development using
PHP & MySQL
UNIT - 3


Date Functions

2
PHP Date and Time

- Prof. Nirav Suthar

3
Date and Time Functions

The date/time functions allow you to
get the date and time from the
server where your PHP script runs.

You can then use the date/time
functions to format the date and
time in several ways.
Date & Time functions

Date() ●
date_add()

Mktime() ●
date_sub()

Strtotime() ●
date_time_set()

date_create() ●
Getdate()

date_format() ●
time()

date_diff()

Date interval format

date_modify()

date_parse()
Date() function

The PHP date() function formats a timestamp to a more
readable date and time.

Syntax:
date(format,timestamp) ;

format - Required. Specifies the format of the timestamp


timestamp - Optional. Specifies a timestamp.

Default is the current date and time


A timestamp is a sequence of characters, denoting the date
and/or time at which a certain event occurred.
Date() function
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)
l (lowercase 'L') - Represents the day of the week

Other characters, like"/", ".", or "-" can also be inserted between the
characters to add additional formatting.
Date() function

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

A timestamp is a numeric value in seconds
between the current time and value as at 1st
January, 1970 00:00:00 Greenwich Mean Time
(GMT).

The value returned by the time function
depends on the default time zone.

The default time zone is set in the php.ini file.

It can also be set programmatically using
date_default_timezone_set function.
timestamp

“$timezone_identifiers= DateTimeZone::listIdentifiers();”

calls the listIdentifiers static method of
the DateandTime Zone built in class.

The listIdentifiers method returns a list
of constants that are assigned to the
variable $timezone_identifiers.
Automatic Copyright Year

Use the date() function to automatically


update the copyright year on your
website:

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


Get a Simple Time
Here are some characters that are 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)

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


Create a Date With PHP
mktime()
The mktime() function returns the Unix timestamp
for a date.

Syntax
mktime(hour,minute,second,month,day,year)

$d=mktime(11, 14, 54, 8, 12, 2014);


echo "Created date is " . date("Y-m-d h:i:sa", $d);
Create a Date From a String With
PHP strtotime()
The PHP strtotime() function is used to convert a
human readable string to a Unix time.
Syntax
strtotime(time,now)

The example below creates a date and time from


the strtotime() function:

$d=strtotime("10:30pm April 15 2014");


echo "Created date is " . date("Y-m-d h:i:sa", $d);
strtotime()

Be aware of dates in the m/d/y or d-m-y
formats;

if the separator is a slash (/), then the
American m/d/y is assumed.

If the separator is a dash (-) or a dot (.),
then the European d-m-y format is
assumed.

To avoid potential errors, you should YYYY-
MM-DD dates.
Write a PHP script to display
the dates for the next six
Saturdays.
<?php
$startdate = strtotime("Saturday");
$enddate = strtotime("+6 weeks", $startdate);

while ($startdate < $enddate) {


echo date("M d", $startdate) . "<br>";
$startdate = strtotime("+1 week", $startdate);
}
?>
date_create()

The date_create() function returns a new


DateTime object.
Syntax
date_create(time);

$date=date_create("2013-03-15");
echo date_format($date,"Y/m/d");
date_format()
The date_format() function returns a date formatted according
to the specified format.
Syntax:
date_format(object,format);

Procedural Style
Example:
<?php
$date=date_create("2013-03-15");
echo date_format($date,"Y/m/d H:i:s");
?>
date_format()
Object oriented style
<?php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s');
?>

date formats
• d - The day of the month (from 01 to 31)
• D - A textual representation of a day (three letters)
• j - The day of the month without leading zeros (1 to 31)
• l (lowercase 'L') - A full textual representation of a day
• N - The ISO-8601 numeric representation of a day (1
for Monday, 7 for Sunday)
• S - The English ordinal suffix for the day of the month
(2 characters st, nd, rd or th. Works well with j)
• w - A numeric representation of the day (0 for Sunday,
6 for Saturday)
date formats
z - The day of the year (from 0 through 365)
W - The ISO-8601 week number of year (weeks starting on
Monday)
F - A full textual representation of a month (January through
December)
m - A numeric representation of a month (from 01 to 12)
M - A short textual representation of a month (three letters)
n - A numeric representation of a month, without leading zeros
(1 to 12)
t - The number of days in the given month
date formats
• L - Whether it's a leap year (1 if it is a leap year, 0
otherwise)
• Y - A four digit representation of a year
• y - A two digit representation of a year
• a - Lowercase am or pm
• A - Uppercase AM or PM
• g - 12-hour format of an hour (1 to 12)
date formats
• G - 24-hour format of an hour (0 to 23)
• h - 12-hour format of an hour (01 to 12)
• H - 24-hour format of an hour (00 to 23)
• i - Minutes with leading zeros (00 to 59)
• s - Seconds, with leading zeros (00 to 59)
• u - Microseconds (added in PHP 5.2.2)
• e - The timezone identifier (Examples: UTC, GMT,
Atlantic/Azores)
• R - Sign "-" when negative, "+" when positive
• r - Sign "-" when negative, empty when positive
date_diff()
The date_diff() function returns the difference
between two DateTime objects.
Syntax:
date_diff(datetime1,datetime2,absolute);
Example:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
?>
DateInterval::format

DateInterval::format — Formats the interval

public string DateInterval::format ( string $format )


- Formats the interval.

Parameters:
format
The following characters are recognized in the
format parameter string. Each format character
must be prefixed by a percent sign (%).
Date Interval Format
Date Interval Format

The format starts with the letter P, for "period."

Each duration period is represented by an integer value
followed by a period designator.

If the duration contains time elements, that portion of the
specification is preceded by the letter T.
Here are some simple examples.

Two days is P2D.

Two seconds is PT2S.

Six years and five minutes is P6YT5M.
date_modify()

The date_modify() function modifies the timestamp.


Synatx:
date_modify(object,modify);
Example:
<?php
$date=date_create("2013-05-01");
date_modify($date,"+15 days");
echo date_format($date,"Y-m-d");
?>
date_parse()

The date_parse() function returns an associative array


with detailed information about a specified date.
Syntax:
date_parse(date);
Example:
<?php
print_r(date_parse("2013-05-01 12:30:45.5"));
?>
date_add()
The date_add() function adds some days, months,
years, hours, minutes, and seconds to a date.
Syntax:
date_add(object,interval);
Example:
<?php
$date=date_create("2013-03-15");
date_add($date,date_interval_create_from_date_string
("40 days"));
echo date_format($date,"Y-m-d");
?>
date_add()
<?php
$date = new DateTime('2000-01-20');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
date_add()

Durations define the amount of intervening time in a time
interval and are represented by the format;

P: period

Y: years

M: months

D: days

T: time

H: hours

M: minutes

S: seconds

So P30D = 30 Days
date_sub()
The date_sub() function subtracts some days, months, years,
hours, minutes, and seconds from a date.
Syntax:
date_sub(object,interval);
Example:
<?php
$date=date_create("2013-03-15");
date_sub($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
?>
date_time_set()
The date_time_set() function sets the time.

Syntax:
date_time_set(object,hour,minute,second);
Example:
<?php
$date=date_create("2013-05-01");
date_time_set($date,13,24);
echo date_format($date,"Y-m-d H:i:s");
?>
getdate()

The getdate() function returns date/time information


of a timestamp or the current local date/time.
Syntax:
getdate(timestamp);
Example:
<?php
print_r(getdate());
?>
time()
The time() function returns the current time in the
number of seconds since the Unix Epoch (January 1
1970 00:00:00 GMT).
Syntax:
time();
Example:
<?php
$t=time();
echo($t . "<br>");
echo(date("Y-m-d",$t));
?>

You might also like