Formatting Dates in PHP

Bradley Miller bradmiller at dslonramp.com
Wed May 15 04:18:40 CDT 2002


At 08:42 PM 5/14/02 -0700, you wrote:
>Hello:
>
>I would like to know how I can use dates in PHP.
>
>This is what I would like to accomplish:
>
>First I would like to assign a starting date, for
>example:
>5/11/02.
>
>I would like to add seven days to it so it will be
>Sat. May 18th and one day so it can be Sun. May 19th.
>
>I would like to do this for every week. At the same
>time, assign a week number according to the date. For
>example; May 11th and 12th would be week #1. Now, May
>18th and 19th would be week #2 and so on...
>
>I know it will be a problem when the month changes. In
>other words what I like to create is a schedule thats
>in a database and loaded according to the week_no.

I think a solution could be crafted by looking at the week number (1-52)
and working from your starting point to your ending point. 

The newest version of PHP added this:

"W - ISO-8601 week number of year, weeks starting on Monday (added in PHP
4.1.0) "

Prior to version 4.1 here's a little script:

"madsen_at_sjovedyr.dk
06-Feb-2002 01:28 
 
In order to retrieve the week number in older versions of PHP (<4.1),
you can, on Unix-like systems, do it this way. (Unless PHP is running in
safe mode.)

<?
function getWeek(){
    $week = `date +%-W`;
    return intval($week)+1;
}
echo getWeek();
?>

This will return the current week (Note that the 'intval($week)+1' makes
sure we start at week number 1 and not at 0 like the date-function on
*nix's.)
Also note the backtick operators, it's not quotationmarks but backticks."

Here's info from the MySQL end of things:

DATE_FORMAT(date,format) 
Formats the date value according to the format string. The following
specifiers may be used in the format string: 
%M  Month name (January..December)  
%W  Weekday name (Sunday..Saturday)  
%D  Day of the month with English suffix (1st, 2nd, 3rd, etc.)  
%Y  Year, numeric, 4 digits  
%y  Year, numeric, 2 digits  
%X  Year for the week where Sunday is the first day of the week, numeric,
4 digits, used with '%V'  
%x  Year for the week, where Monday is the first day of the week, numeric,
4 digits, used with '%v'  
%a  Abbreviated weekday name (Sun..Sat)  
%d  Day of the month, numeric (00..31)  
%e  Day of the month, numeric (0..31)  
%m  Month, numeric (01..12)  
%c  Month, numeric (1..12)  
%b  Abbreviated month name (Jan..Dec)  
%j  Day of year (001..366)  
%H  Hour (00..23)  
%k  Hour (0..23)  
%h  Hour (01..12)  
%I  Hour (01..12)  
%l  Hour (1..12)  
%i  Minutes, numeric (00..59)  
%r  Time, 12-hour (hh:mm:ss [AP]M)  
%T  Time, 24-hour (hh:mm:ss)  
%S  Seconds (00..59)  
%s  Seconds (00..59)  
%p  AM or PM  
%w  Day of the week (0=Sunday..6=Saturday)  
%U  Week (0..53), where Sunday is the first day of the week  
%u  Week (0..53), where Monday is the first day of the week  
%V  Week (1..53), where Sunday is the first day of the week. Used with
'%X'  
%v  Week (1..53), where Monday is the first day of the week. Used with
'%x'  
%%  A literal `%'.




More information about the Kclug mailing list