Skip to content

Instantly share code, notes, and snippets.

@abdullahbutt
Created December 2, 2013 13:10
Show Gist options
  • Select an option

  • Save abdullahbutt/7749190 to your computer and use it in GitHub Desktop.

Select an option

Save abdullahbutt/7749190 to your computer and use it in GitHub Desktop.
date in php
<!DOCTYPE html>
<html>
<head>
<title>PHP Important Date Functions</title>
</head>
<body>
<p>For PHP Date Functions, visit <a href="http://www.w3schools.com/php/php_ref_date.asp">W3schools PHP 5 Date/Time Functions</a></p>
<p>For PHP Calendar Functions, visit <a href="http://www.w3schools.com/php/php_ref_calendar.asp">W3schools PHP 5 Calendar Functions</a></p>
<?php
echo '<strong>'.'date_create( ) Function:'.'</strong>'.'<br>';
$date1=date_create("2013-12-02");
var_dump($date1);
$date2=date_create("1983-08-27");
var_dump($date2);
echo $date1->date.'<br>';
echo $date2->date.'<br>';
echo '<br>'.'<strong>'.'date_format( ) Function:'.'</strong>'.'<br>'.'<br>';
echo date_format($date1,"Y/m/d");
echo "<br>";
echo date_format($date2,"Y/m/d").'<br>';
$date1_updated=date_format($date1,"Y-m-d");
$date2_updated=date_format($date2,"Y-m-d");
echo '<br>'.'<strong>'.'date_diff( ) Function:'.'</strong>'.'<br>'.'<br>';
$diff=date_diff($date2,$date1);
var_dump($diff);
echo 'Date 1 is '.$date1_updated. ' & date 2 is '.$date2_updated.'<br>';
echo 'The Difference between above 2 dates is:'.'<br>'.
'Years: '.$diff->y.'<br>'.'days: '.$diff->d.'<br>'.
'Months: '.$diff->m.'<br>'.'Count of days: '.$diff->days.'<br>';
echo '<br>'.'<strong>'.'format( ) Function:'.'</strong>'.'<br>'.'<br>';
echo $diff->format("%R%a days");
echo "<br>";
echo "<br>";
//Subtract 40 days from the 15th of March, 2013:
echo '<strong>'.'date_sub( ) Function to subtract date from current date:'.'</strong>'.'<br>'.'<br>';
echo '<strong>'.'Subtract 40 days from the 15th of March, 2013:'.'</strong>'.'<br>';
$date=date_create("2013-03-15");
echo 'Date 40 days before '.date_format($date, "Y-m-d").' is:';
echo "<br>";
date_sub($date,date_interval_create_from_date_string("40 days"));
echo date_format($date,"Y-m-d");
echo '<br>'.'<br>'.'<strong>'.'PHP strtotime( ) Function:'.'</strong>'.'<br>'.'<br>';
echo 'Definition and Usage:
The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT).
Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.
Note: 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 or date_create_from_format() when possible'.'<br>'.'<strong>'.'Syntax:'.'</strong>'.'<br>'.'strtotime(time,now)'.'<br>'.
'time-->Required. Specifies a date/time string'.'<br>'.
'now--> Optional. Specifies the timestamp used as a base for the calculation of relative dates'.'<br>'.'<br>';
echo(strtotime("now") . "<br>");
echo(strtotime("3 October 2005") . "<br>");
echo(strtotime("+5 hours") . "<br>");
echo(strtotime("+1 week") . "<br>");
echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>");
echo(strtotime("next Monday") . "<br>");
echo(strtotime("last Sunday")).'<br>';
echo '<br>'.'<br>'.'<strong>'.'PHP date_create_from_format() Function'.'</strong>'.'<br>'.'<br>';
$date=date_create_from_format("j-M-Y","15-Mar-2013");
echo date_format($date,"Y/m/d");
echo '<br>'.'<br>'.
'<strong>'.'Definition and Usage:'.'</strong>'.'<br>'.'<br>'.
'The date_create_from_format() function returns a new DateTime object formatted according to the specified format.'.'Syntax'.'<br>'.'date_create_from_format(format,time,timezone);'.'<br>'.'time-->Required. Specifies a date/time string. NULL indicates the current date/time'.'<br>'.'timezone-->Optional. Specifies the timezone of time. Default is the current timezone'.'<br>'.
'format-->Required'.'<br>'.'Specifies the format to use. The following characters can be used in the format parameter string:' .'<br>'.'
d - Day of the month; with leading zeros' .'<br>'.'
j - Day of the month; without leading zeros' .'<br>'.'
D - Day of the month (Mon - Sun)' .'<br>'.'
l - Day of the month (Monday - Sunday)' .'<br>'.'
S - English suffix for day of the month (st, nd, rd, th)' .'<br>'.'
F - Monthname (January - December)' .'<br>'.'
M - Monthname (Jan-Dec)' .'<br>'.'
m - Month (01-12)' .'<br>'.'
n - Month (1-12)' .'<br>'.'
Y - Year (e.g 2013)' .'<br>'.'
y - Year (e.g 13)' .'<br>'.'
a and A - am or pm' .'<br>'.'
g - 12 hour format with leading zeros' .'<br>'.'
h - 12 hour format without leading zeros' .'<br>'.'
G - 24 hour format with leading zeros' .'<br>'.'
H - 12 hour format without leading zeros' .'<br>'.'
i - Minutes with leading zeros' .'<br>'.'
s - Seconds with leading zeros' .'<br>'.'
u - Microseconds (up to six digits)' .'<br>'.'
e, O, P and T - Timezone identifier' .'<br>'.'
U - Seconds since Unix Epoch' .'<br>'.'
(space)' .'<br>'.'
# - One of the following separation symbol: ;,:,/,.,,,-,(,) '.'<br>'.'
? - A random byte' .'<br>'.'
* - Rondom bytes until next separator/digit' .'<br>'.'
! - Resets all fields to Unix Epoch' .'<br>'.'
| - Resets all fields to Unix Epoch if they have not been parsed yet' .'<br>'.'
+ - If present, trailing data in the string will cause a warning, not an error'
;
?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment