Simplier version of gmgetdate
function gmgetdate($timestamp = null)
{
if ($timestamp) return getdate($timestamp);
else return getdate(time());
}
Note: A unix timestamp is always represented in GMT (or more correctly UCT)
getdate
(PHP 4, PHP 5)
getdate — Retourne la date/heure
Description
Retourne un tableau associatif contenant les informations de date et d'heure du timestamp timestamp lorsqu'il est fourni, sinon, le timestamp de la date/heure courante locale.
Liste de paramètres
- timestamp
-
Le paramètre optionnel timestamp est un timestamp Unix de type entier qui vaut par défaut l'heure courante locale si le paramètre timestamp n'est pas fourni. En d'autres termes, il faut par défaut la valeur de la fonction time().
Valeurs de retour
Retourne un tableau associatif contenant les informations de date et d'heure du timestamp timestamp . Les éléments du tableau associatif retourné sont les suivants :
| Clé | Description | Exemple de valeur retournée |
|---|---|---|
| "seconds" | Représentation numérique des secondes | 0 à 59 |
| "minutes" | Représentation numérique des minutes | 0 à 59 |
| "hours" | Représentation numérique des heures | 0 à 23 |
| "mday" | Représentation numérique du jour du mois courant | 1 à 31 |
| "wday" | Représentation numérique du jour de la semaine courante | 0 (pour Dimanche) à 6 (pour Samedi) |
| "mon" | Représentation numérique du mois | 1 à 12 |
| "year" | Année, sur 4 chiffres | Exemples : 1999 ou 2003 |
| "yday" | Représentation numérique du jour de l'année | 0 à 365 |
| "weekday" | Version texte du jour de la semaine | Sunday à Saturday |
| "month" | Version texte du mois, comme January ou March | January à December |
| 0 | Nombre de secondes depuis l'époque Unix, similaire à la valeur retournée par la fonction time() et utilisée par date(). | Dépend du système, typiquement de -2147483648 à 2147483647. |
Exemples
Exemple #1 Exemple avec getdate()
<?php
$today = getdate();
print_r($today);
?>
L'exemple ci-dessus va afficher quelque chose de similaire à :
Array ( [seconds] => 40 [minutes] => 58 [hours] => 21 [mday] => 17 [wday] => 2 [mon] => 6 [year] => 2003 [yday] => 167 [weekday] => Tuesday [month] => June [0] => 1055901520 )
getdate
12-Sep-2008 05:45
08-Aug-2008 04:08
For those who want the gmgetdate function that matches the behaviour of getdate:
<?php
function gmgetdate($timestamp = null) {
if (is_null($timestamp)) { $timestamp = time(); }
$dateParts = array(
'mday' => 'j',
'wday' => 'w',
'yday' => 'z',
'mon' => 'n',
'year' => 'Y',
'hours' => 'G',
'minutes' => 'i',
'seconds' => 's',
'weekday' => 'l',
'month' => 'F',
0 => 'U'
);
while (list(, $value) = each($dateParts)) {
$value = gmdate($value, $timestamp);
if (is_numeric($value)) { $value = (int)$value; }
}
return $dateParts;
}
?>
10-Jan-2008 05:07
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]
13-Dec-2006 01:38
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..
10-Dec-2006 07:05
A nice little function I wrote to determine what number occurrence weekday it is of the month for a given timestamp. (I.e. 2nd Friday, or the 3rd Thursday)
Eg: print_r(getWeekdayOccurrence(mktime(0, 0, 0, 12, 1, 2006)));
Outputs: Array ( [0] => 1 [1] => Friday ) [The first friday]
Eg. print_r(getWeekdayOccurrence(mktime(0, 0, 0, 8, 17, 2009)));
Outputs: Array ( [0] => 3 [1] => Monday ) [The third Monday]
<?php
function getWeekdayOccurrence($time) {
$month = intval(date("m", $time)); $day = intval(date("d", $time));
for ($i = 0; $i < 7; $i++) {
$days[] = date("l", mktime(0, 0, 0, $month, ($i+1), date("Y", $time)));
}
$posd = array_search(date("l", $time), $days);
$posdm = array_search($days[0], $days) - $posd; /
return array((($day+$posdm+6)/7), $days[$posd]);
}
?>
22-Oct-2006 05: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());
?>
04-Mar-2006 02:47
<?php
// 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 ;
}
?>
11-May-2005 02:17
getting weekday(actual date) from any give date.
<?php
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;
}
?>
21-Apr-2004 11: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;
} ?>
15-Sep-2003 03: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";
?>
