The function strtotime( ) understands words about
the current time:
$a = strtotime('now');
print strftime('%c',$a);
$a = strtotime('today');
print strftime('%c',$a);
Mon Aug 12 20:35:10 2002
Mon Aug 12 20:35:10 2002
It understands different ways to identify a time and date:
$a = strtotime('5/12/1994');
print strftime('%c',$a);
$a = strtotime('12 may 1994');
print strftime('%c',$a);
Thu May 12 00:00:00 1994
Thu May 12 00:00:00 1994
It understands relative times and dates:
$a = strtotime('last thursday'); // On August 12, 2002
print strftime('%c',$a);
$a = strtotime('2001-07-12 2pm + 1 month');
print strftime('%c',$a);
Thu Aug 8 00:00:00 2002
Mon Aug 12 14:00:00 2002
It understands time zones. When the following is run from a computer
in EDT, it prints out the same time:
$a = strtotime('2002-07-12 2pm edt + 1 month');
print strftime('%c',$a);
Mon Aug 12 14:00:00 2002
However, when the following is run from a computer in EDT, it prints
out the time in EDT when it is 2 P.M. in MDT (two hours before EDT):
$a = strtotime('2002-07-12 2pm mdt + 1 month');
print strftime('%c',$a);
Mon Aug 12 16:00:00 2002
$date = '1974-12-03 05:12:56';
preg_match('/(\d{4})-(\d{2})-(\d{2}) (\d{2}):(\d{2}):(\d{2})/',$date,$date_parts);
This puts the year, month, day, hour, minute, and second into
$date_parts[1] through
$date_parts[6]. (preg_match( )
puts the entire matched expression into
$date_parts[0].)
You can use regular expressions to pull the date and time out of a
larger string that might also contain other information (from user
input, or a file you're reading), but if
you're sure about the position of the date in the
string you're parsing, you can use substr(
) to make it even faster:
$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);
You can also use split( );
$ar = split('[- :]',$date);
print_r($ar);
Array
(
[0] => 1974
[1] => 12
[2] => 03
[3] => 05
[4] => 12
[5] => 56
)