PHP Manipulate Date
From w3cyberlearnings
Manipulate Date
strtotime() function to convert human readable dates into unix timestamps
$linux_timestamp1 =strtotime("now"); $linux_timestamp2 =strtotime("today"); $linux_timestamp3 =strtotime("tomorrow"); $linux_timestamp4 =strtotime("yesterday"); $linux_timestamp5 =strtotime("+2 hours"); $linux_timestamp6 =strtotime("-2 month"); $linux_timestamp7 =strtotime("2 weeks ago"); $linux_timestamp8 =strtotime("30 seconds"); $linux_timestamp9 =strtotime("+2 years -3 months"); $linux_timestamp10 =strtotime("10 September 2000"); $linux_timestamp11 =strtotime("+1 day"); $linux_timestamp12 =strtotime("+1 week"); $linux_timestamp13 =strtotime("+1 week 2 days 4 hours 2 seconds"); $linux_timestamp14 =strtotime("next Thursday"); $linux_timestamp15 =strtotime("last Monday");
date() function
Char |
Example |
Result |
Description |
Year | |||
y | $y1=date('y'); | 12 | Two digit year |
Y | $y2=date('Y'); | 2012 | Four digit year |
Month | |||
F | $m1=date('F'); | January | Full month name (January, February, May..) |
M | $m2=date('M'); | Jan | Three letter abbreviation month (Jan, Feb, Mar..) |
m | $m3=date('m'); | 01 | Two digit numeric month (01,02,03..) |
n | $m4=date('n'); | 1 | No zero numeric month (1,2,3,4..) |
Day of Month | |||
d | $d1=date('d'); | 01 | Two digit day of month |
j | $d2=date('j'); | 1 | No zero day of month |
S | $d3=date('S'); | 1st | Two digit day and include two chracters (st, nd, th) |
Day of Week | |||
l | $d4=date('l'); | Monday | Full day of the day of the week |
D | $d5=date('D'); | Mon | Three characters day of the week |
w | $d6=date('w'); | 0 | Numeric representation of day of the week (0=Sunday, 6=Saturday) |
Hour | |||
h | $h1=date('h'); | 02 | Two digit hour (12 hour format) |
H | $h2=date('H'); | 21 | Two digit hour (24 hour format) |
g | $h3=date('g'); | 1 | No leading zero hour (12 hour format) |
G | $h4=date('G'); | 1 | No leading zero hour(24 hour format) |
a | $h5=date('a'); | 5 am | am/pm (lower case format) |
A | $h6=date('A'); | 5 AM | AM/PM (upper case format) |
Minute | |||
i | $m=date('i'); | 02 | two digit and leading zero minute format |
j | $m2=date('j'); | 2 | without leading zero minute |
Second | |||
s | $s1=date('s'); | 04 | Second with leading zero (two digits) |
Z | $s2=date('Z'); | 4 | Second without leading zero |
explode() function to get the date and hour
<?php $date = '2012-10-03 08:10:57'; $date_aa = explode(' ',$date); // get date only echo 'Date: ' .$date_aa[0] . '<br/>'; // get hour only echo 'Hour: ' .$date_aa[1] . '<br/>'; ?>
Output
Date: 2012-10-03 Hour: 08:10:57
explode to separate the date into year, day, and month
$date_only = explode('-',$date_aa[0]); foreach($date_only as $d) { echo $d . '<br/>'; }
Output
2012 10 03
explode to separate the hour into hour, minute, and second
$hr_only = explode(':',$date_aa[1]); foreach($hr_only as $d) { echo $d . '<br/>'; }
Output
08 10 57
use substr to separate the date into year, day, month, hour, minute, and second
$date = '2012-10-03 08:10:57'; $date_parts[0] = substr($date,0,4); $date_parts[1] = substr($date,5,2); $date_parts[2] = substr($date,8,2); $date_parts[3] = substr($date,11,2); $date_parts[4] = substr($date,14,2); $date_parts[5] = substr($date,17,2);
Using strtotime() and date() function to manipulate date and time
<?php $today = strtotime("today"); $tomorrow = strtotime("tomorrow"); echo date("F d, Y", $today) . '<br/>'; echo date("F d, Y", $tomorrow); ?>
Output
April 17, 2012 April 18, 2012
Example 1
<?php $two2ago = strtotime("2 days ago"); $next2days = strtotime("+2 days"); echo date("F d, Y", $two2ago) . '<br/>'; echo date("F d, Y", $next2days); ?>
Output
April 15, 2012 April 19, 2012