PHP Unconference Europe 2015

getdate

(PHP 4, PHP 5)

getdateRiceve informazioni su data/orario

Descrizione

array getdate ([ int $timestamp ] )

Restituisce un array associativo contenente le informazioni sulla data del timestamp, o dell'attuale orario locale se non รจ stato assegnato timestamp, con i seguenti elementi di array:

  • "seconds" - secondi
  • "minutes" - minuti
  • "hours" - ore
  • "mday" - giorno del mese
  • "wday" - giorno della settimana, numerico : da 0 come Domenica a 6 come Sabato
  • "mon" - mese, numerico
  • "year" - anno, numerico
  • "yday" - giorno dell'anno, numerico; i.e. "299"
  • "weekday" - giorno della settimana, testuale, per intero; i.e. "Friday"
  • "month" - mese, testuale, per intero; i.e. "January"

Example #1 Esempio di getdate()

$today = getdate(); 
$month = $today['month']; 
$mday = $today['mday']; 
$year = $today['year']; 
echo "$month $mday, $year";

add a note add a note

User Contributed Notes 4 notes

up
9
Yura Pylypenko (plyrvt at mail dot ru)
11 years ago
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";
?>
up
4
andre at anlex dot co dot za
7 years ago
I thought best to show a posseble way to go about bypassing the end month issue where the first day in a new month will have the monday of the week that it falls in - in the old month. Use the numbering of days as the constant and work you way from there.

Example:
<?php
//-----------------------------
$now = time();
$num = date("w");
if (
$num == 0)
{
$sub = 6; }
else {
$sub = ($num-1); }
$WeekMon  = mktime(0, 0, 0, date("m", $now)  , date("d", $now)-$sub, date("Y", $now));    //monday week begin calculation
$todayh = getdate($WeekMon); //monday week begin reconvert

$d = $todayh[mday];
$m = $todayh[mon];
$y = $todayh[year];
echo
"$d-$m-$y"; //getdate converted day

?>

Allot less code makes everyone happy..
up
-13
eric dot schultz at NOSPAM dot CyVon dot com
5 years ago
Here is another gmgetdate that is a little faster/suscint (no loops).

<?php
function gmgetdate2($ts = null){
       
$k = array('seconds','minutes','hours','mday',
               
'wday','mon','year','yday','weekday','month',0);
        return(
array_combine($k,split(":",
               
gmdate('s:i:G:j:w:n:Y:z:l:F:U',is_null($ts)?time():$ts))));
        }
?>

It also returns the values in the same order as getdate.
up
-20
timforte at gmail dot com
6 years ago
It's worth noting that this is local time, not UTC/GMT - gmgetdate doesn't exist :(.

The most logical way to handle date arithmetic without hitting DST problems is to work in UTC...

<?php
function add_days($my_date,$numdays) {
 
$date_t = strtotime($my_date.' UTC');
  return
gmdate('Y-m-d',$date_t + ($numdays*86400));
}
?>

[it's even faster if you use gmmktime instead of strtotime]
To Top