PHP Date and Time Functions

Date and Time Functions

Image result for 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() function :-

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

 Syntax: date(format,timestamp) ; 

format - Required. Specifes the format of the timestamp timestamp - Optional. Specifes 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.


The required format parameter of the date() function specifes 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.


<?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 fle. It can also be set programmatically using date_default_timezone_set function.


“$timezone_identifers= DateTimeZone::listIdentifers();”
- calls the listIdentifers static method of the DateandTime Zone built in class.
- The listIdentifers method returns a list of constants that are assigned to the
variable $timezone_identifers.

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 specifed 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');
?>

Image result for date_format() in php

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 sufx 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
- 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)

- 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 identifer (Examples: UTC, GMT,Atlantic/Azores)
- R - Sign "-" when negative, "+" when positive
- r - Sign "-" when negative, empty when positive

date_dif():-
The date_dif() function returns the diference between two DateTime objects.
Syntax:-
date_dif(datetime1,datetime2,absolute);
Example:
<?php
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$dif=date_dif($date1,$date2);
echo $dif->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 ormat parameter string. Each format character must be prefxed by a percent sign (%).

date_modify():-
The date_modify() function modifes 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 specifed 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");
?>

<?php
$date = new DateTime('2000-01-20');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>


- Durations defne 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));
?>

Post a Comment

0 Comments