strptime

(PHP 5 >= 5.1.0RC1)

strptime -- 解析由 strftime() 生成的日期/时间

说明

array strptime ( string date, string format )

strptime() 返回一个将 date 解析后的数组,如果出错返回 FALSE

月份和星期几的名字以及其它与语种有关的字符串对应于 setlocale()设定的当前区域(LC_TIME)。

参数

datestring

被解析的字符串(例如从 strftime() 返回的)

formatstring

date 所使用的格式(例如同 strftime() 中所使用的相同)。

更多有关格式选项的信息见 strftime() 页面。

返回值

返回一个数组,失败则返回 FALSE

表格 1. 数组中包含以下单元

键名说明
tm_sec当前分钟内的秒数(0-61)
tm_min当前小时内的分钟数(0-59)
tm_hour午夜起的小时数(0-23)
tm_mday月份中的第几天(1-31)
tm_mon自一月起过了几个月(0-11)
tm_year自 1900 年起过了几年
tm_wday自星期天起过了几天(0-6)
tm_yday本年自一月一日起过了多少天(0-365)
unparseddate 中未能通过指定的 format 识别的部分

范例

例子 1. strptime() 例子

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

echo
"$strf\n";

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

上例的输出类似于:

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] =>
)

参见

strftime()


add a note add a note User Contributed Notes
jojyjob at gmail dot com
13-May-2006 03:18
/***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
"<br>";
 }

//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."<br>"; 
//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))."<BR>";
  
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."<br>"; 
//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))."<BR>";
  
array_push($pre,$dat1);
  
//echo $dat1;
  
$datstart=$dat1;
  
$rep--;
  }
  return
$pre;
}

?>
Malte Starostik
28-Mar-2006 02:45
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().
erik at ackerholm dot se
04-Feb-2006 06:55
Note that elements that are not found (since they don't exist in the specified format) will NOT be set to null.

Also a two-digit-year (found with %y) is returned as 1969-2068 (or 69-168 since this function returns years since 1900).

<?php
$format
= '%y-%m-%d';
$testdate = '69-04-03';
$founddate = strptime ($testdate, $format);
var_dump ($founddate);
?>

returns

array(9) {
  ["tm_sec"]=>
  int(-1212975008)
  ["tm_min"]=>
  int(28)
  ["tm_hour"]=>
  int(140)
  ["tm_mday"]=>
  int(3)
  ["tm_mon"]=>
  int(3)
  ["tm_year"]=>
  int(69)
  ["tm_wday"]=>
  int(4)
  ["tm_yday"]=>
  int(92)
  ["unparsed"]=>
  string(0) ""
}