PHP
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

getdate> <date_timezone_set
Last updated: Fri, 18 Jul 2008

view this page in

date

(PHP 4, PHP 5)

date — Formatta una data o orario locale

Descrizione

string date ( string $formato [, int $ timestamp ] )

Restituisce una stringa formattata in accordo con il formato della stringa usato nell' intero timestamp o nell'attuale orario locale se timestamp non è assegnato.

Nota: Il valido intervallo del timestamp è abitualmente da Fri, 13 Dec 1901 20:45:54 GMT a Tue, 19 Jan 2038 03:14:07 GMT. (Queste date corrispondono al valore minimo e al massimo per un intero segnato a 32-bit.)
Per generare un timestamp da una stringa rappresentante la data, devi sapere usare strtotime(). In aggiunta, dei databases hanno funzioni che convertono i loro formati di data in timestamps (come la funzione di MySQL, UNIX_TIMESTAMP).

I seguenti caratteri sono utilizzati nella stringa formato:

  • a - "am" o "pm"
  • A - "AM" o "PM"
  • B - Swatch Internet time
  • d - giorno del mese, 2 cifre senza tralasciare gli zero; i.e. "01" a "31"
  • D - giorno della settimana, testuale, 3 lettere; i.e. "Fri"
  • F - mese, testuale, long; i.e. "January"
  • g - ora, formato a 12-ore senza eventuali zero; i.e. "1" a "12"
  • G - ora, formato a 24-ore senza eventuali zero; i.e. "0" a "23"
  • h - ora, formato a 12-ore; i.e. "01" a "12"
  • H - ora, formato a 24-ore; i.e. "00" a "23"
  • i - minuti; i.e. "00" a "59"
  • I (i grande) - "1" se c'è l'ora legale, "0" altrimenti.
  • j - giorno del mese senza eventuali zero; i.e. "1" a "31"
  • l ('L' piccola) - giorno della settimana, testuale, long; i.e. "Friday"
  • L - valore booleano per stabilire se è un anno bisestile; i.e. "0" o "1"
  • m - mese; i.e. "01" a "12"
  • M - mese, testuale, 3 lettere; i.e. "Jan"
  • n - mese senza eventuali zero; i.e. "1" a "12"
  • O - Differenza in ore dal fuso orario Greenwich; i.e. "+0200"
  • r - Data formattata RFC 822; i.e. "Thu, 21 Dec 2000 16:01:07 +0200" (aggiunto nel PHP 4.0.4)
  • s - secondi; i.e. "00" a "59"
  • S - Suffisso ordinale Inglese per i giorni del mese, 2 caratteri; i.e. "th", "nd"
  • t - numero di giorni del mese dato; i.e. "28" a "31"
  • T - Fuso orario di questo computer; i.e. "MDT"
  • U - secondi dall'epoca since the epoch
  • w - giorno della settimana, numerico, i.e. "0" (Domenica) a "6" (Sabato)
  • W - ISO-8601 Numero della settimana dell'anno, le settimane iniziano il lunedì (aggiunto in PHP 4.1.0) (Sabato)
  • Y - anno, 4 cifre; i.e. "1999"
  • y - anno, 2 cifre; i.e. "99"
  • z - giorno dell'anno; i.e. "0" a "365"
  • Z - Fuso orario in secondi (i.e. "-43200" a "43200"). Il fuso orario ad ovest dell'UTC è sempre negativo, e per quelli ad est è sempre positivo.
Caratteri non utilizzati nella stringa del formato saranno scritti come sono. Il formato "Z" restituirà sempre "0" quando si usa gmdate().

Example #1 Esempio di date()

echo date ("l dS of F Y h:i:s A");
echo "July 1, 2000 is on a " . date ("l", mktime(0,0,0,7,1,2000));

Puoi utilizzare un carattere utilizzabile nella stringa del formato come carattere normale facendolo semplicemente precedere dal carattere di escape, il backslash. Se il carattere con un backslash è ancora una sequenza speciale, devi inserire di nuovo il carattere di escape per il backslash.

Example #2 Caratteri di escape in date()

echo date("l \\t\h\e jS"); // scrive qualcosa tipo 'Saturday the 8th'

È possibile usare date() e mktime() assieme per cercare delle date nel futuro o nel passato.

Example #3 Esempio di date() e mktime()

$tomorrow  = mktime (0,0,0,date("m")  ,date("d")+1,date("Y"));
$lastmonth = mktime (0,0,0,date("m")-1,date("d"),  date("Y"));
$nextyear  = mktime (0,0,0,date("m"),  date("d"),  date("Y")+1);

Nota: Questo può essere più affidabile della semplice addizione e sottrazione del numero di secondi in un giorno o mese a un timestamp a causa del daylight savings time.

Alcuni esempi della formattazione di date(). Nota che puoi scrivere qualsiasi altro carattere senza escape, il quale attualmente non dovrebbe produrre risultati indesiderati, ma altri caratteri potrebbero essere assegnati come caratteri della stringa di formattazione nelle prossime versioni del PHP. Quando si utilizza il carattere di escape, assicurati di usare un singolo apice per prevenire che caratteri come \n facciano iniziare una nuova linea.

Example #4 Formattazione con date()

/* Today is March 10th, 2001, 5:16:18 pm */
$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm
$today = date("m.d.y");                         // 03.10.01
$today = date("j, n, Y");                       // 10, 3, 2001
$today = date("Ymd");                           // 20010310
$today = date('h-i-s, j-m-y, it is w Day z ');  // 05-16-17, 10-03-01, 1631 1618 6 Fripm01
$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // It is the 10th day.
$today = date("D M j G:i:s T Y");               // Sat Mar 10 15:16:08 MST 2001
$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:17 m is month
$today = date("H:i:s");                         // 17:16:17

Per formattare le date in lingue diverse dall'inglese, dovresti usare le funzioni setlocale() e strftime().

Guarda anche getlastmod(), gmdate(), mktime(), strftime() e time().



getdate> <date_timezone_set
Last updated: Fri, 18 Jul 2008
 
add a note add a note User Contributed Notes
date
JonathanCross.com
25-Jul-2008 01:22
<?php
// A demonstration of the new DateTime class for those
// trying to use dates before 1970 or after 2038.
?>
<h2>PHP 2038 date bug demo (php version <?= phpversion() ?>)</h1>
<div style='float:left;margin-right:3em;'>
<h3>OLD Buggy date()</h3>
<?
  $format='F j, Y';
  for ( $i = 1900; $i < 2050; $i++) {
    $datep = "$i-01-01";
    ?>
    Trying: <?=$datep?> = <?=date($format, strtotime($datep))?><br>
    <?
  }
?></div>
<div style='float:left;'>
  <h3>NEW DateTime Class (v 5.2+)</h3><?
  for ( $i = 1900; $i < 2050; $i++) {
    $datep = "$i-01-01";
    $date = new DateTime($datep);
    ?>
    Trying: <?=$datep?> = <?=$date->format($format)?><br>
    <?
  }
?></div>
gertrude dot mendoza at gmail dot com
18-Jul-2008 12:41
<?php
/*
@params $firstdate, $lastdate
@return array() of array(monday,sunday)
@description returns all the mondays and sundays of the given date range
*/
function get_week_intervals($fdate,$ldate)
{
    list(
$year,$month,$day) = explode('-',$fdate);
   
$daynum = date('w',
                  
mktime(date('H'),
                         
date('i'),
                         
date('s'),
                         
$month,
                         
$day,
                         
$year)
                  );
   
$daynum = $daynum==0? 7 : $daynum;
   
$week=array();
   
//get the dayname of the first day
    //if month = current month get the current date as the last day
   
if($month==date('m'))
    {
       
$lastday = date('d');
    }
    else
    {
       
$lastday = date('t', strtotime($fdate));
    }
    if((
date('l',strtotime($fdate))) == 'Sunday')
    {
       
$monday = $fdate;
       
$sunday = $fdate;
    }
    else
    {
       
$monday = $fdate;
       
$sunday = date('Y-m-d',(mktime(date('H'),
                      
date('i'),date('s'),$month,
                      
$day,$year))-($daynum-7)*86400);

    }
   
$week[] = array('monday'=>$monday,'sunday'=>$sunday);

   
$day = date('d',strtotime($sunday." +1 day"));

    while(
$sunday < $ldate)
    {
       
$monday = date('Y-m-d',strtotime($sunday." +1 day"));

        list(
$year,$month,$day) = explode('-',$monday);
       
$daynum = date('w',
                     
mktime(date('H'),
                            
date('i'),
                            
date('s'),
                            
$month,
                            
$day,
                            
$year)
                       );
       
$daynum = $daynum==0? 7 : $daynum;

       
$sunday = date('Y-m-d',(mktime(date('H'),date('i'),
                      
date('s'),$month,$day,$year))-($daynum-7)*86400);
        if(
$sunday > $ldate)
        {
           
$sunday = $ldate;
        }

       
$week[] = array('monday'=>$monday,'sunday'=>$sunday);
    }

    return
$week;
}
?>
Anonymous
17-Jul-2008 09:30
to get the date of the monday from a week these function is useful:

<?php
/* Get First Calendar Week of Year x*/
 
function firstkw($jahr)
    {
      
$erster = mktime(0,0,0,1,1,$jahr);
      
$wtag = date('w',$erster);
    if (
$wtag <= 4) {
          
/**
            * Donnerstag oder kleiner: auf den Montag zurückrechnen.
            */
          
$montag = mktime(0,0,0,1,1-($wtag-1),$jahr);
       } else {
          
/**
            * auf den Montag nach vorne rechnen.
            */                                                                        
          
$montag = mktime(0,0,0,1,1+(7-$wtag+1),$jahr);
       }
       return
$montag;
    }

/*GET timestamp of monday of month x*/
   
function mondaykw($kwtime)
    {
          
$firstmonday =$this->firstkw(date("Y", $kwtime));
          
$mon_monat = date('m',$firstmonday);
          
$mon_jahr = date('Y',$firstmonday);
          
$mon_tage = date('d',$firstmonday);
          
$tage = (date("W", $kwtime)-1)*7;
          
$mondaykw = mktime(0,0,0,$mon_monat,$mon_tage+$tage,$mon_jahr);
           return
$mondaykw;
    }

echo
mondaykw(time()); //1215986400
?>
fgabrieli at gmail dot com
14-Jul-2008 12:56
here is a function to split a number of seconds in hours, minutes and seconds

<?php

   
function split_seconds($seconds)
    {
       
// get the minutes

       
$minutes = floor($seconds / 60) ;

       
$seconds_left = $seconds % 60 ;

       
// get the hours

       
$hours = floor($minutes / 60) ;

       
$minutes_left = $minutes % 60 ;

       
// (test) show the result 

       
echo "$hours hours $minutes_left minutes and $seconds_left seconds" ;
    }

   
split_seconds(68648) ;

?>
   

best regards
fernando gabrieli
shem_lexluger at yahoo.com
14-Jul-2008 05:43
Assuming a week starts on Monday. To calculate the week number of the current date starting from a specified date, use this function:

<?php
function weekcounter($start_date)
{
  
$start_date = strtotime($start_date);
  
$today = strtotime(date('d-m-Y'));

  
$week = 1;
   while(
$start_date < $today) {
   
$start_date = strtotime('next Monday', $start_date);
   
$week++;
   }

   return
$week;
}

// Example:
//now = 14-07-2008
echo weekcounter('01-07-2008'); //prints 3
echo weekcounter('01-06-2008'); //prints 8 instead of 7; because 1st jun is a Sunday so it is week 1 and 2nd june is week 2. Feel free to tweak.
?>
Rob A.
10-Jul-2008 09:38
Quick function for returning the names of the next 7 days of the week starting with today.

Returns an array that can be formatted to your liking.

<?php
/**
* Returns array of next 7 days starting with today
*
*/

function next_7_days() {
       
// create array of day names. You can change these to whatever you want
   
$days = array(
                           
'Monday',
                           
'Tuesday',
                           
'Wednesday',
                           
'Thursday',
                           
'Friday',
                           
'Saturday',
                           
'Sunday');
   
$today = date('N');
    for (
$i=1;$i<$today;$i++) {

               
// take the first element off the array
       
$shift = array_shift($days);

               
// ... and add it to the end of the array
       
array_push($days,$shift);
    }
       
// returns the sorted array
   
return $days;
}
?>

It basically takes an array starting with Monday and shifts each day to the end of the array until the first element in the array is today.
con_tobe at yahoo dot com
09-Jul-2008 08:46
Doing $w-- for months ending on Sat won't hurt (i.e. if you're counting weeks as is the case below), but halocastle's code is perfectly fine as is and quite fast.  He/she uses $w as a key for the $weeks array.  "Halo" does this BEFORE $w++, so $w-- is superfluous as the loop has already ended.  For May, 2008, I get 5 weeks as expected...

Array
(
    [1] => Array
        (
            [4] => 1
            [5] => 2
            [6] => 3
        )

    [2] => Array
        (
            [0] => 4
            [1] => 5

------------OMITTED-----------------

            [4] => 22
            [5] => 23
            [6] => 24
        )

    [5] => Array
        (
            [0] => 25
            [1] => 26
            [2] => 27
            [3] => 28
            [4] => 29
            [5] => 30
            [6] => 31
        )

)

I guess the one pit-fall of the code is if you overlap months, say the following year, then $m-- makes perfect since...I think (haven't gotten that far...yet).

I modified "Halo's" code to include months, too (this is from a snippet that produces a three month calendar, hence the outer $months loop, omitted here).

<?php
$m
= date('m');
$Y = date('Y');

// for() {months loop omitted
$var_date = mktime(0, 0, 0, $m, 1, $Y);
$month_name = date('F', $var_date);
$months[$month_name]['DAYS'] = date('t', $var_date);
$months[$month_name]['FIRST_DAY'] = date('w', $var_date);
//}
foreach($months as $month => $key) {
 
$weeks = array();
  for(
$i = 1, $j = $key['FIRST_DAY'], $w = 1;$i <= $key['DAYS'];$i++) {
   
$weeks[$w][$j] = $i;
   
$j++;
    if(
$j == 7) {
     
$j = 0;
     
$w++;
    }
  }
 
$months[$month]['WEEKS'] = $weeks;
}
?>

Enjoy!
dmagick at gmail dot com
02-Jul-2008 07:44
Slight amendment to halocastle at yahoo dot com 's code as it doesn't take into account when a month finishes on a Saturday (eg May 2008).

<?php
$start_date
= mktime(0, 0, 0,$start_month, 1, $start_year);

$days_in_month = date('t', $start_date);
$month_first_day = date('w', $start_date);

$j = $month_first_day;
$num_weeks = 1;

for(
$i = 1; $i <= $days_in_month; $i++) {
   
$j++;
    if(
$j == 7) {
       
$j = 0;
       
$num_weeks++;
    }
}

// if the last day of the month happens to be a Saturday,
// take one off the number of weeks
// because it was being added inside the for loop.
if ($j == 0) {
   
$num_weeks--;
}
?>
halocastle at yahoo dot com
30-Jun-2008 09:20
Weeks and days for any month/year combo:

<?php
$m
= 2; // February
$Y = 2008;

// constants used here for legibility, use $vars for dynamicon...
define('MONTH_DAYS',date('t', strtotime(date($m . '/01/' . $Y))));
// w:0->6 = Sun->Sat
define('MONTH_FIRST_DAY',date('w', strtotime(date($m . '/01/' . $Y))));

for(
$i = 1, $j = MONTH_FIRST_DAY, $w = 1;$i <= MONTH_DAYS;$i++) {
 
$week[$w][$j] = $i;
 
$j++;
  if(
$j == 7) {
   
$j = 0;
   
$w++;
  }
}
?>

print_r($week):
-----------------------
Array
(
    [1] => Array
        (
            [5] => 1
            [6] => 2
        )

    [2] => Array
        (
            [0] => 3
            [1] => 4
            [2] => 5
            [3] => 6
            [4] => 7
            [5] => 8
            [6] => 9
        )

    [3] => Array
        (
            [0] => 10
            [1] => 11
            [2] => 12
            [3] => 13
            [4] => 14
            [5] => 15
            [6] => 16
        )

    [4] => Array
        (
            [0] => 17
            [1] => 18
            [2] => 19
            [3] => 20
            [4] => 21
            [5] => 22
            [6] => 23
        )

    [5] => Array
        (
            [0] => 24
            [1] => 25
            [2] => 26
            [3] => 27
            [4] => 28
            [5] => 29
        )

)
Kavi Siegel
29-Jun-2008 07:18
I wrote the following function to show a series of drop down boxes to select the date. When provided with a timestamp, that date is selected by default, when none is provided, the current date is selected.

<?php
function chooseDate($timestamp = ""){
    if(
$timestamp == ""){
       
$timestamp = time();
    }
   
$months = array(null, 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    unset(
$months[0]);
   
print_r($months);
   
$out = '<select name="month">';
    foreach(
$months as $key => $month){
        if(
$month == date('M', $timestamp)){
           
$out .= '<option value="'.$key.'" selected="selected">'.$month.'</option>';
        }else{
           
$out .= '<option value="'.$key.'">'.$month.'</option>';
        }
    }
   
$out .= '</select><select name="days">';
    for(
$i = 1; $i <= 32; $i++){
        if(
$i == date('j', $timestamp)){
           
$out .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
        }else{
           
$out .= '<option value="'.$i.'">'.$i.'</option>';
        }
    }
   
$out .= "</select><select name='year'>";
    for(
$i = date('Y'); $i >= 1970; $i--){
        if(
$i == date('Y', $timestamp)){
           
$out .= '<option value="'.$i.'" selected="selected">'.$i.'</option>';
        }else{
           
$out .= '<option value="'.$i.'">'.$i.'</option>';
        }
    }
   
$out .= "</select>";
    return
$out;
}
?>

Usage is simple:

<?php
echo chooseDate(); // Will select current date
echo chooseDate(1149566400); // Will select June 6th, 2006
?>
aplarsen
26-Jun-2008 01:12
@anonymous (12-Jun-2008 08:45):
date("t") returns the last day of the month, not the last working day of the month.

A cleaner example would be as follows:
<?php
function lastworkingday($date)
{   
   for(
$lastday=mktime(0,0,0,date("m",$date),
      
date("t",$date),date("Y",$date));
    
date("w",$lastday)==0 || date("w",$lastday)==6;
    
$lastday-=60*60*24);
   return
date("j",$lastday);
}
?>
kontakt at arthur minus schiwon dot de
18-Jun-2008 03:29
to get the week of the month simply use:
ceil( date("j") / 7 );
diego at diego dot eng dot br
09-Jun-2008 04:27
I made a small code to get the last working day of the month:

<?php

$times
= strtotime(date("Y")."-".date("m")."-".date("t"));
for (
$lastworkingday=0;$lastworkingday==0;$times-=86400)
   if (
date("w",$times)!=0 && date("w",$times)!=6) $lastworkingday = date("j",$times);
print
$lastworkingday;

?>
phil dot taylor at enilsson dot com
25-May-2008 10:37
Found this helpful when converting unix dates for use with the ical file format.

<?php
// Converts a unix timestamp to iCal format (UTC) - if no timezone is
// specified then it presumes the uStamp is already in UTC format.
// tzone must be in decimal such as 1hr 45mins would be 1.75, behind
// times should be represented as negative decimals 10hours behind
// would be -10
       
   
function unixToiCal($uStamp = 0, $tzone = 0.0) {
   
       
$uStampUTC = $uStamp + ($tzone * 3600);       
       
$stamp  = date("Ymd\THis\Z", $uStampUTC);
       
        return
$stamp;       

    }
?>
chubby at chicks dot com
23-May-2008 06:54
<?php
/**
     * Checks wether a date is between an interval
     *
     * Usage:
     *     
     * // check if today is older than 2008/12/31
     * var_dump(currentDayIsInInterval('2008/12/31'));
     * // check if today is younger than 2008/12/31
     * var_dump(currentDayIsInInterval(null,'2008/12/31'));
     * // check if today is between 2008/12/01 and 2008/12/31
     * var_dump(currentDayIsInInterval('2008/12/01','2008/12/31')); 
     *
     * Will trigger errors if date is in wrong format, notices if $begin > $end    
     *         
     * @param string $begin Date string as YYYY/mm/dd
     * @param string $end Date string as YYYY/mm/dd
     * @return bool 
     */
function currentDayIsInInterval($begin = '',$end = '')
{
       
$preg_exp = '"[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]"';
       
$preg_error = 'Wrong parameter passed to function '.__FUNCTION__.' : Invalide date
format. Please use YYYY/mm/dd.'
;
       
$interval_error = 'First parameter in '.__FUNCTION__.' should be smaller than
second.'
;
        if(empty(
$begin))
        {
               
$begin = 0;
        }
        else
        {
                if(
preg_match($preg_exp,$begin))
                {
                       
$begin = (int)str_replace('/','',$begin);
                }
                else
                {
                       
trigger_error($preg_error,E_USER_ERROR);
                }
        }
        if(empty(
$end))
        {
               
$end = 99999999;
        }
        else
        {
                if(
preg_match($preg_exp,$end))
                {
                       
$end = (int)str_replace('/','',$end);
                }
                else
                {
                       
trigger_error($preg_error,E_USER_ERROR);
                }
        }
        if(
$end < $begin)
        {
               
trigger_error($interval_error,E_USER_WARNING);
        }
       
$time = time();
       
$now = (int)(date('Y',$time).date('m',$time).date('j',$time));
        if(
$now > $end or $now < $begin)
        {
                return
false;
        }
        return
true;
}
?>
wilson dot p dot pereira at itelefonica dot com dot br
21-May-2008 08:37
The function below extracts any date time value basead in the string format.
Returns an associative array with day,month,year,hour,min and seg separated.

<?php
function ExtractDateTimeByFormat($strDateTime, $strFormat="dmYHis")
{
 
//extract the format
 
$i = 0;
 
$aFieldOrder = array();
 
$nFields = 0;
       
$strExtraction = "";
        while(isset(
$strFormat[$i]))
        {
           
$strField = $strFormat[$i];
            switch (
strtolower($strField) )
            {
                case
"D";
                case
"d";
                   
$aFieldOrder[$nFields] = "d";
                   
$nFields++;
                   
$strExtraction .= "%d";
                    if(isset(
$strFormat[$i+1]))
                    {
                       
$strExtraction .= "%*1c";
                    }
                break;

                case
"M";
                case
"m";
                   
$aFieldOrder[$nFields] = "m";
                   
$nFields++;
                   
$strExtraction .= "%d";
                    if(isset(
$strFormat[$i+1]))
                    {
                       
$strExtraction .= "%*1c";
                    }
                break;

                case
"y";
                case
"Y";
                   
$aFieldOrder[$nFields] = "y";
                   
$nFields++;
                   
$strExtraction .= "%4d";
                    if(isset(
$strFormat[$i+1]))
                    {
                       
$strExtraction .= "%*1c";
                    }
                break;

                case
"h";
                case
"H";
                   
$aFieldOrder[$nFields] = "h";
                   
$nFields++;
                   
$strExtraction .= "%d";
                    if(isset(
$strFormat[$i+1]))
                    {
                       
$strExtraction .= "%*1c";
                    }
                break;

                case
"i";
                   
$aFieldOrder[$nFields] = "i";
                   
$nFields++;
                   
$strExtraction .= "%d";
                    if(isset(
$strFormat[$i+1]))
                    {
                       
$strExtraction .= "%*1c";
                    }
                break;

                case
"S";
                case
"s";
                   
$aFieldOrder[$nFields] = "s";
                   
$nFields++;
                   
$strExtraction .= "%d";
                    if(isset(
$strFormat[$i+1]))
                    {
                       
$strExtraction .= "%*1c";
                    }
                break;
            }
           
$i++;
        }

   
$aValues = array();
   
$aValues = sscanf($strDateTime,$strExtraction);

    return
array_combine($aFieldOrder,$aValues);

}
?>
wulf dot kaiser at mpimf-heidelberg dot mpg dot de
21-May-2008 01:00
For output formatting of a SAMP based seminar announcement system, i had to fetch the date of every friday of a given month in a given year. Here's what i did:

<?php

$givenYear
= $_GET["givenYear"]; # assume "2006"
$givenMonth = $_GET["givenMonth"]; # assume "12"

if ($givenMonth != '12') {

   
$nextGivenMonth = "1";
   
$nextGivenYear = $givenYear + 1;}

    else {

       
$nextGivenMonth = $givenMonth + 1;
       
$nextGivenYear = $givenYear;}
       
       
# Get the first weekday of the month

       
$firstDayOfMonth = date("d", mktime(0, 0, 0, $givenMonth, 1, $givenYear));
       
$firstWeekDayOfMonth = date("l", mktime(0, 0, 0, $givenMonth, 1, $givenYear));
       
       
# Count days to first Friday

       
switch ($firstWeekDayOfMonth) {

            case
'Monday': $numOfDaysToFirstFriday = "4"; break;
            case
'Tuesday': $numOfDaysToFirstFriday = "3"; break;
            case
'Wednesday': $numOfDaysToFirstFriday = "2"; break;
            case
'Thursday': $numOfDaysToFirstFriday = "1"; break;
            case
'Friday': $numOfDaysToFirstFriday = "0"; break;
            case
'Saturday': $numOfDaysToFirstFriday = "6"; break;
            case
'Sunday': $numOfDaysToFirstFriday = "5"; break;}
           
           
# Get first Friday's date

           
$numOfDaysToFirstFriday = 1 + $numOfDaysToFirstFriday;
           
$firstFridayOfMonthDate = date("d.m.Y", mktime(0, 0, 0, $givenMonth, $numOfDaysToFirstFriday, $givenYear));
           
$firstFridayOfMonthDay = date("d", mktime(0, 0, 0, $givenMonth, $numOfDaysToFirstFriday, $givenYear));
           
           
# Get the last weekday of the month

           
$lastDayOfMonth date("d", strtotime("-1 day", strtotime(date("$nextGivenYear-$nextGivenMonth-01"))));
           
$lastWeekDayOfMonth date("l", strtotime("-1 day", strtotime(date("$nextGivenYear-$nextGivenMonth-01"))));
           
           
# Count days to last Friday

           
switch ($lastWeekDayOfMonth) {

                case
'Monday': $numOfDaysToLastFriday = "3"; break;
                case
'Tuesday': $numOfDaysToLastFriday = "4"; break;
                case
'Wednesday': $numOfDaysToLastFriday = "5"; break;
                case
'Thursday': $numOfDaysToLastFriday = "6"; break;
                case
'Friday': $numOfDaysToLastFriday = "0"; break;
                case
'Saturday': $numOfDaysToLastFriday = "1"; break;
                case
'Sunday': $numOfDaysToLastFriday = "2"; break;}
               
               
# Get last Friday's date

               
$numOfDaysToLastFriday = $lastDayOfMonth - $numOfDaysToLastFriday;
               
$lastFridayOfMonthDate = date("d.m.Y", mktime(0, 0, 0, $givenMonth, $numOfDaysToLastFriday, $givenYear));
               
$lastFridayOfMonthDay = date("d", mktime(0, 0, 0, $givenMonth, $numOfDaysToLastFriday, $givenYear));

               
$divisor = $lastFridayOfMonthDay - $firstFridayOfMonthDay;
               
$divisor = $divisor / 7;
                global
$divisor;
               
               
# Get the dates of all Fridays in the given Month (can be either 4 or 5)

               
if ($divisor=='3') {

                   
$firstFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth, $numOfDaysToFirstFriday, $givenYear));
                   
$secondFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth, $lastFridayOfMonthDay - 14, $givenYear));
                   
$thirdFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth, $lastFridayOfMonthDay - 7, $givenYear));
                   
$lastFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth, $numOfDaysToLastFriday, $givenYear));}

                    else if (
$divisor=='4') {

                       
$firstFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth, $numOfDaysToFirstFriday, $givenYear));
                       
$secondFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth, $lastFridayOfMonthDay - 21, $givenYear));
                       
$thirdFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth, $lastFridayOfMonthDay - 14, $givenYear));
                       
$fourthFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth, $lastFridayOfMonthDay - 7, $givenYear));
                       
$lastFridayOfMonth = date("Y/m/d", mktime(0, 0, 0, $givenMonth, $numOfDaysToLastFriday, $givenYear));}

?>

Comments, suggestions and bugfixes are welcome ;-))
Raymond Irving
25-Mar-2008 08:44
I have written a little Date Class Library for PHP that supports timestamps for dates greater than 2038 and lesser than 1970.

This library can be used in both PHP4 and PHP5.

Check it out here:

http://xwisdomhtml.com/dateclass.html
ash at atomic-network dot co dot uk
20-Mar-2008 09:26
in regards to: [takzik at ebanat dot com]

si