strtotime


time apg

PARSES about any ENGLISH textual date-time description into a UNIX timestamp.


May experience difficulties in displaying characters outside of the English language when preceded with the setlocale function.




<?php

int
|false strtotime ( str $datetime [, int|null $baseTimestamp = null ] )


where,

$datetime = A date/time STRING according to the general format

$baseTimestamp 
= The timestamp to be used as a base 
                               
for the calculation of relative dates

?>

 $datetime 


A date/time STRING according to the general format.



 $baseTimestamp 


The timestamp to be used as a base for the calculation of relative dates.





Returns the UNIX timestamp on SUCCESS, FALSE otherwise.

Older versions of PHP this function would return -1 on failure.

This function expects to be given a string containing an ENGLISH date format and will try to parse that format into a UNIX timestamp, that is, the number of seconds since January 1 1970 00:00:00 UTC, relative to the timestamp given in $baseTimestamp, or the current time if $baseTimestamp is not supplied.

Each parameter of this function uses the default Time Zones unless a time zone is specified in that parameter.

Be careful not to use different Time Zones in each parameter unless that is intended.

See date_default_timezone_set and date_default_timezone_get on the various ways to define the default time zone.



  1 EXERCISE   

<?php

$loc_FR 
= '"fr_FR.utf-8", "fr.utf-8", "fra", "french"';
setlocale(LC_ALL, "fr_FR.utf-8", "fr.utf-8", "fra", "french");
date_default_timezone_set('Europe/Paris');
$defa_tz = date_default_timezone_get(); 

/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

   The locale in this example is only used by the strftime()

   La locale dans cet exemple est utilisé uniquement 
   par la fonction strftime ()
   
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */

echo $defa_tz . '<br>';
$timstp_01 = strtotime("Feb 13,1980 11:32:30");
echo 
'strtotime("Feb 13,1980 11:32:30") = ' . $timstp_01 . '<br>';;
$timstp_02 = strtotime("February 13, 1980 11:32:30");
echo 
'strtotime("February 13,1980 11:32:30") = ' . $timstp_02 . 
 
'<br><br><br>';

$timstp_03 = strtotime("now");
echo 
'strtotime("now") = ' . $timstp_03 .  '<br>';;
echo 
'strftime("%A, %B/%d/%Y %Hh %Mmin %Ss"'. ', '
 
. $timstp_03 .')<br>';
$dt_timstp_03 = strftime("%A, %B/%d/%Y %Hh %Mmin %Ss",
 
$timstp_03);
echo 
$dt_timstp_03 . '<br><br><br>';

$timstp_04 = strtotime("+2 days");
echo 
'strtotime("+2 days") = ' . $timstp_04 .  '<br>';
echo 
'strftime("%A, %B/%d/%Y %Hh %Mmin %Ss"'. ', '
 
. $timstp_04 .')<br>';
$dt_timstp_04 = strftime("%A, %B/%d/%Y %Hh %Mmin %Ss",
 
$timstp_04);
echo 
$dt_timstp_04 . '<br><br><br>';

$timstp_05 = strtotime("-8 years");
echo 
'strtotime("-8 years") = ' . $timstp_05 .  '<br>';
echo 
'strftime("%A, %B/%d/%Y %Hh %Mmin %Ss"'. ', '
 
. $timstp_05 .')<br>';
$dt_timstp_05 = strftime("%A, %B/%d/%Y %Hh %Mmin %Ss",
 
$timstp_05);
echo 
$dt_timstp_05 . '<br><br>';

?>

  2 EXERCISE   

<?php

echo strtotime("now") . '<br>';
echo 
date("l F d Y H:i:s", strtotime("now")) . '<br><br>'; 

echo 
strtotime("-1 year") . '<br>';
echo 
date("l F d Y H:i:s", strtotime("-1 year")) . '<br><br>';

echo 
strtotime("next sunday") . '<br>';
echo 
date("l F d Y H:i:s", strtotime("next sunday")) . '<br><br>'; 

echo 
strtotime("last saturday") . '<br>';
echo 
date("l F d Y H:i:s", strtotime("last saturday")) . '<br><br>';

echo 
strtotime("+1 month 2 weeks 1 days 6 hours 30 minutes") . '<br>';
echo 
date("l F d Y H:i:s", 
strtotime("+1 mont 2 weeks 1 days 6 hours 30 minutes")) . '<br><br>';

?>