getdate

(PHP 3, PHP 4, PHP 5)

getdate -- 取得日期/时间信息

说明

array getdate ( [int timestamp] )

返回一个根据 timestamp 得出的包含有日期信息的结合数组。如果没有给出时间戳则认为是当前本地时间。数组中的单元如下:

表格 1. 返回的关联数组中的键名单元

键名说明返回值例子
"seconds"秒的数字表示059
"minutes"分钟的数字表示059
"hours"小时的数字表示023
"mday"月份中第几天的数字表示131
"wday"星期中第几天的数字表示0(表示星期天)到 6(表示星期六)
"mon"月份的数字表示112
"year"4 位数字表示的完整年份例如:19992003
"yday"一年中第几天的数字表示0365
"weekday"星期几的完整文本表示SundaySaturday
"month"月份的完整文本表示January>December
0 自从 Unix 纪元开始至今的秒数,和 time() 的返回值以及用于 date() 的值类似。 系统相关,典型值为从 -21474836482147483647

例子 1. getdate() 例子

<?php
$today
= getdate();
print_r($today);
?>

上例的输出类似于:

Array
(
    [seconds] => 40
    [minutes] => 58
    [hours]   => 21
    [mday]    => 17
    [wday]    => 2
    [mon]     => 6
    [year]    => 2003
    [yday]    => 167
    [weekday] => Tuesday
    [month]   => June
    [0]       => 1055901520
)

参见 date()time()setlocale()


add a note add a note User Contributed Notes
cesar at nixar dot org
22-Oct-2006 11:49
<?php

 
/**
   *  This function is similar to getdate() but it returns
   * the month information.
   *
   *  Returns an associative array containing the month
   * information of the parameters, or the current month
   * if no parameters are given.
   *
   */

 
function getmonth ($month = null, $year = null)
  {
    
// The current month is used if none is supplied.
    
if (is_null($month))
        
$month = date('n');

    
// The current year is used if none is supplied.
    
if (is_null($year))
        
$year = date('Y');

    
// Verifying if the month exist
    
if (!checkdate($month, 1, $year))
         return
null;

    
// Calculating the days of the month
    
$first_of_month = mktime(0, 0, 0, $month, 1, $year);
    
$days_in_month = date('t', $first_of_month);
    
$last_of_month = mktime(0, 0, 0, $month, $days_in_month, $year);

    
$m = array();
    
$m['first_mday'] = 1;
    
$m['first_wday'] = date('w', $first_of_month);
    
$m['first_weekday'] = strftime('%A', $first_of_month);
    
$m['first_yday'] = date('z', $first_of_month);
    
$m['first_week'] = date('W', $first_of_month);
    
$m['last_mday'] = $days_in_month;
    
$m['last_wday'] = date('w', $last_of_month);
    
$m['last_weekday'] = strftime('%A', $last_of_month);
    
$m['last_yday'] = date('z', $last_of_month);
    
$m['last_week'] = date('W', $last_of_month);
    
$m['mon'] = $month;
    
$m['month'] = strftime('%B', $first_of_month);
    
$m['year'] = $year;

     return
$m;
  }

 
// Output
 
print_r(getmonth(11, 1978));
 
print_r(getmonth());

?>
John Sherwood
15-May-2006 02:10
In response to the "Simple routine for determining whether a date in mySQL format has gone past":

function pastdate($t)
{
   if (strtotime($t) < time())
     return false;
   return true;
}

or you could use

mysql_select("SELECT UNIX_TIMESTAMP(fieldname) FROM tablename"), which would give you the date in seconds since unix epoch, and which you could compare to time().
Cas_AT_NUY_DOT_INFO
04-Mar-2006 09:47
// This functions calculates the next date only using business days
// 2 parameters, the startdate and the number of businessdays to add
   function calcduedate($datecalc,$duedays) {
   $i = 1;
   while ($i <= $duedays) {
       $datecalc += 86400; // Add a day.
       $date_info  = getdate( $datecalc );
       if (($date_info["wday"] == 0) or ($date_info["wday"] == 6) )  {
           $datecalc += 86400; // Add a day.
           continue;
       }
       $i++;
   }
   return $datecalc ;
   }
leo25in at yahoo dot com
11-May-2005 08:17
getting weekday(actual date) from any give date.

function cal_date($wday,$tstamp)
{
   return $tstamp-($wday*(24*3600));
}

function getweekday($m,$d,$y)
{
   $tstamp=mktime(0,0,0,$m,$d,$y);
  
   $Tdate = getdate($tstamp);
  
   $wday=$Tdate["wday"];
  
   switch($wday)
   {
       case 0;
       $wstamp=cal_date($wday,$tstamp);
       //echo date("Y-m-d",$wstamp);
       break;
      
       case 1;
       $wstamp=cal_date($wday,$tstamp);
       //echo date("Y-m-d",$wstamp);
       break;
      
      
       case 2;
       $wstamp=cal_date($wday,$tstamp);
       //echo date("Y-m-d",$wstamp);
       break;
      
       case 3;
       $wstamp=cal_date($wday,$tstamp);
       //echo date("Y-m-d",$wstamp);
       break;
      
       case 4;
       $wstamp=cal_date($wday,$tstamp);
       //echo date("Y-m-d",$wstamp);
       break;
      
       case 5;
       $wstamp=cal_date($wday,$tstamp);
       //echo date("Y-m-d",$wstamp);
       break;
      
       case 6;
       $wstamp=cal_date($wday,$tstamp);
       //echo date("Y-m-d",$wstamp);
       break;
   }
  
  
     $w["day"]=date("d",$wstamp);
     $w["month"]=date("m",$wstamp);
     $w["year"]=date("Y",$wstamp);
    
     return $w;

}
Liis make
16-Sep-2004 06:22
function win2unix($date_string,$date_timestamp)
{

   $epoch_diff = 11644473600; // difference 1601<>1970 in seconds. see reference URL
   $date_timestamp = $date_timestamp * 0.0000001;
   $unix_timestamp = $date_timestamp - $epoch_diff;
   echo date($date_string,$unix_timestamp);   
}
getisomonday($year, $week)
22-Apr-2004 05:58
getdate does not convert week numbers. this function relies on strftime to find a timestamp that falls on the monday of specified year and ISO week:

<?php function getisomonday($year, $week) {
      
# check input
      
$year = min ($year, 2038); $year = max ($year, 1970);
      
$week = min ($week, 53); $week = max ($week, 1);
      
# make a guess
      
$monday = mktime (1,1,1,1,7*$week,$year);
      
# count down to week
      
while (strftime('%V', $monday) != $week)
              
$monday -= 60*60*24*7;
      
# count down to monday
      
while (strftime('%u', $monday) != 1)
              
$monday -= 60*60*24;
      
# got it
      
return $monday;
}
?>
Yura Pylypenko (plyrvt at mail dot ru)
15-Sep-2003 09:29
In addition to canby23 at ms19 post:
It's a very bad idea to consider day having 24 hours (86400 secs), because some days have 23, some - 25 hours due to daylight saving changes. Using of mkdate() and strtotime() is always preferred. strtotime() also has a very nice behaviour of datetime manipulations:
<?php
echo strtotime ("+1 day"), "\n";
echo
strtotime ("+1 week"), "\n";
echo
strtotime ("+1 week 2 days 4 hours 2 seconds"), "\n";
echo
strtotime ("next Thursday"), "\n";
echo
strtotime ("last Monday"), "\n";
?>
intronis
12-Sep-2003 02:04
When adding a timestamp to a database from php, be carefule because if your DB Server is a different computer than your webserver, the NOW() function in the SQL will use the DB Server's time, and not the web server's. You can use NTP to synch the times on both computers.
logan dot hall at asu dot edu
12-Mar-2002 04:32
It seems that 'yday' (the day of the year) that php produces is one less than what the unix 'date +%j' produces.

Not sure why this is, but I would guess that php uses 0-365 rather than 1-366 like unix does.  Just something to be careful of.
ih2 at morton-fraser dot com
17-Oct-2000 08:01
To do comparisons on dates stored in mysql db against 7 days ago, 1 month ago etc use the following:
$last_week = date("Y-m-d", mktime(0,0,0, date(m), date(d)-7,date(Y)));

if($date > $last_week)
   {
     etc.
This allows for intelligent looping i.e. works at start/end of month/year
I have noticed other postings re this and they have not worked for me.Hope this helps.