Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg



PHP : Function Reference : Date and Time Functions : strptime

strptime

Parse a time/date generated with strftime (PHP 5 >= 5.1.0)
array strptime ( string date, string format )

strptime() returns an array with the date parsed, or FALSE on error.

Month and weekday names and other language dependent strings respect the current locale set with setlocale() (LC_TIME).

Note:

This function is not implemented on Windows platforms.

Parameters

date (string)

The string to parse (e.g. returned from strftime())

format (string)

The format used in date (e.g. the same as used in strftime()).

For more information about the format options, read the strftime() page.

Return Values

Returns an array, or FALSE on failure.

Table 45. The following parameters are returned in the array

parameters Description
tm_sec Seconds after the minute (0-61)
tm_min Minutes after the hour (0-59)
tm_hour Hour since midnight (0-23)
tm_mday Day of the month (1-31)
tm_mon Months since January (0-11)
tm_year Years since 1900
tm_wday Days since Sunday (0-6)
tm_yday Days since January 1 (0-365)
unparsed the date part which was not recognized using the specified format


Examples

Example 462. strptime() example

<?php
$format
= '%d/%m/%Y %H:%M:%S';
$strf = strftime($format);

echo
"$strf\n";

print_r(strptime($strf, $format));
?>

The above example will output something similar to:

03/10/2004 15:54:19

Array
(
   [tm_sec] => 19
   [tm_min] => 54
   [tm_hour] => 15
   [tm_mday] => 3
   [tm_mon] => 9
   [tm_year] => 104
   [tm_wday] => 0
   [tm_yday] => 276
   [unparsed] =>
)


See Also
strftime()

Code Examples / Notes » strptime

chad 0x40 herballure 0x2e com

The result of strptime() is not affected by the current timezone setting, even though strftime() is. Tested in PHP 5.1.6.

malte starostik

It says "Parse a time/date generated with strftime()" but that's not entirely correct -- While strptime("2006131", "%Y%W%u") works as expected, strptime("2006131", "%G%V%u") returns false instead of reversing the equivalent - and unambiguous - strftime() usage.  I suspect that's because glibc doesn't support that.  Anyway, this docu page fails to mention that apparently not all format components supported by strftime() can be used with strptime().

svenr

If you need strptime but are restricted to a php version which does not support it (windows or before PHP 5), note that MySQL since Version 4.1.1 offers (almost?) the same functionality with the STR_TO_DATE function.
See http://dev.mysql.com/doc/refman/4.1/en/date-and-time-functions.html


dt
//This turns non-standard but often used "datetime" string
//like '20060810084251' into nice formatted date
//'Thursday, 10 August 2006 08:42:51 CEST'
//note, that strptime returns day of year counting from 0, so
//you need to put 1 as month number to get appropriate
//month for the daycount. for 2006 strptime for unknown
//reason returns 106, so I simply add 1900
$informat = '%Y%m%d%H%M%S';
$outformat =  '%A, %d %B %Y %T %Z';
$ftime = strptime("20060810084251",$informat);
$unxTimestamp = mktime(
                   $ftime['tm_hour'],
                   $ftime['tm_min'],
                   $ftime['tm_sec'],
                   1 ,
                   $ftime['tm_yday'] + 1,
                  $ftime['tm_year'] + 1900
                );
//setlocale(LC_TIME,'pl_PL');
echo strftime($outformat , $unxTimestamp );


jojyjob

/***Finding the days of a week ***/
<?php
$out = pre();  
$outpre=nextweek();
$td=date("Y-m-d");
$result = array_reverse($outpre);
//print_r($result);
array_push($result,$td);
$newarray = array_merge($result,$out);
 foreach($newarray as $date1){
   echo $date1;
echo "
";
}
//print_r($out);
//print_r($newarray);
function pre()  
{
$monP=0;
$tueP=1;
$wedP=2;
$thuP=3;
$friP=4;
$satP=5;
$sunP=6;
 
$td=date("Y-m-d");  
//echo $td;
$tdname=date("l");  
 switch($tdname)
 {
  case "Monday":
      $rep=$monP;
  break;
  case "Tuesday":
      $rep=$tueP;
  break;
  case "Wednesday":
      $rep=$wedP;
  break;
  case "Thursday":
      $rep=$thuP;
  break;
  case "Friday":
      $rep=$friP;
  break;
  case "Saturday":
      $rep=$satP;  
  break;
  case "Sunday":
      $rep=$sunP;  
  break;  
  default:
      echo "Sorry";  
 }

//echo $tdname."
";  
//echo $rep;
$datstart =$td;  /* the starting date */
//$rep = 12;  /* number of future dates to display */
$nod = 1;  /* number of days in the future to increment the date */
$nom = 0;  /* number of months in the future to increment the date */
$noy = 0;  /* number of years in the future to increment the date */
$precon=future_date($datstart,$rep,$nod,$nom,$noy);
return $precon;
}
function future_date($datstart,$rep,$nod,$nom,$noy) {
$pre = array();
 while ($rep >= 1) {
   $datyy=substr($datstart,0,4);
   $datmm=substr($datstart,5,2);
   $datdd=substr($datstart,8,2);
   $fda=$datdd - $nod;
   $fmo=$datmm - $nom;
   $fyr=$datyy -$noy;
   $dat1=date("Y-m-d", mktime(0,0,0,$fmo,$fda,$fyr))."
";
array_push($pre,$dat1);
   //echo $dat1;
   $datstart=$dat1;
   $rep--;
 }
 return $pre;
}
function nextweek()
{
$monN=6;
$tueN=5;
$wedN=4;
$thuN=3;
$friN=2;
$satN=1;
$sunN=0;
$td=date("Y-m-d");  
$tdname=date("l");  
 switch($tdname)
 {
  case "Monday":
      $rep=$monN;
  break;
  case "Tuesday":
      $rep=$tueN;
  break;
  case "Wednesday":
      $rep=$wedN;
  break;
  case "Thursday":
      $rep=$thuN;
  break;
  case "Friday":
      $rep=$friN;
  break;
  case "Saturday":
      $rep=$satN;  
  break;
  case "Sunday":
      $rep=$sunN;  
  break;  
  default:
      echo "Sorry";  
 }

//echo $tdname."
";  
//echo $rep;
$datstart =$td;  /* the starting date */
//$rep = 12;  /* number of future dates to display */
$nod = 1;  /* number of days in the future to increment the date */
$nom = 0;  /* number of months in the future to increment the date */
$noy = 0;  /* number of years in the future to increment the date */
$con = future_date1($datstart,$rep,$nod,$nom,$noy);
return $con;
}
function future_date1($datstart,$rep,$nod,$nom,$noy) {
$pre = array();
 while ($rep >= 1) {
   $datyy=substr($datstart,0,4);
   $datmm=substr($datstart,5,2);
   $datdd=substr($datstart,8,2);
   $fda=$datdd + $nod;
   $fmo=$datmm + $nom;
   $fyr=$datyy + $noy;
   $dat1=date("Y-m-d", mktime(0,0,0,$fmo,$fda,$fyr))."
";
array_push($pre,$dat1);
   //echo $dat1;
   $datstart=$dat1;
   $rep--;
 }
 return $pre;
}
?>


Change Language


Follow Navioo On Twitter
checkdate
date_create
date_date_set
date_default_timezone_get
date_default_timezone_set
date_format
date_isodate_set
date_modify
date_offset_get
date_parse
date_sun_info
date_sunrise
date_sunset
date_time_set
date_timezone_get
date_timezone_set
date
getdate
gettimeofday
gmdate
gmmktime
gmstrftime
idate
localtime
microtime
mktime
strftime
strptime
strtotime
time
timezone_abbreviations_list
timezone_identifiers_list
timezone_name_from_abbr
timezone_name_get
timezone_offset_get
timezone_open
timezone_transitions_get
eXTReMe Tracker