★ wanayoo — archive 1999 http://www.php.net/manual/function.date.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to Date/time
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
Date/time
* checkdate
* date
* getdate
* gettimeofday
* gmdate
* gmmktime
* gmstrftime
* localtime
* microtime
* mktime
* strftime
* time
* strtotime
Manual: date
View the source code for this pageSearch the site



Previous page
 checkdate
 Updated
Sat, 12 Aug 2000
getdate 
Next page


date

(PHP3 , PHP4 )

date -- Format a local time/date

Description

string date (string format [, int timestamp])

Returns a string formatted according to the given format string using the given timestamp or the current local time if no timestamp is given.

The following characters are recognized in the format string:

  • a - "am" or "pm"

  • A - "AM" or "PM"

  • d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"

  • D - day of the week, textual, 3 letters; i.e. "Fri"

  • F - month, textual, long; i.e. "January"

  • h - hour, 12-hour format; i.e. "01" to "12"

  • H - hour, 24-hour format; i.e. "00" to "23"

  • g - hour, 12-hour format without leading zeros; i.e. "1" to "12"

  • G - hour, 24-hour format without leading zeros; i.e. "0" to "23"

  • i - minutes; i.e. "00" to "59"

  • j - day of the month without leading zeros; i.e. "1" to "31"

  • l (lowercase 'L') - day of the week, textual, long; i.e. "Friday"

  • L - boolean for whether it is a leap year; i.e. "0" or "1"

  • m - month; i.e. "01" to "12"

  • n - month without leading zeros; i.e. "1" to "12"

  • M - month, textual, 3 letters; i.e. "Jan"

  • s - seconds; i.e. "00" to "59"

  • S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd"

  • t - number of days in the given month; i.e. "28" to "31"

  • T - Timezone setting of this machine; i.e. "MDT"

  • U - seconds since the epoch

  • w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)

  • Y - year, 4 digits; i.e. "1999"

  • y - year, 2 digits; i.e. "99"

  • z - day of the year; i.e. "0" to "365"

  • Z - timezone offset in seconds (i.e. "-43200" to "43200")

Unrecognized characters in the format string will be printed as-is. The "Z" format will always return "0" when using gmdate().

Example 1. Date() example


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

It is possible to use date() and mktime() together to find dates in the future or the past.

Example 2. Date() and mktime() example


$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);
      

To format dates in other languages, you should use the setlocale() and strftime() functions.

See also gmdate() and mktime().


User Contributed Notes: date


todd_woodward@bigfoot.com
21-Jul-1999 01:26
$today = date("F j, Y, g:i a");

PRINT "$today CT";



Produces:

July 21, 1999, g:20 pm CT



peter@kantea.it
29-Jul-1999 01:47
Convert Times between Timezones


// First set the Timezone


// (see the zoneinfo directory-tree on your System for a list


// it is in /usr/share/zoneinfo on my System)


putenv("TZ=Europe/Rome");


// Calculate the Timstamp relative to the Timezone


$timestamp=mktime($hour,$minutes,$seconds,$month,$day,$year);


// Show the time


echo "Date ".date("d/m/Y H:i:s",$timestamp)." in ".getenv ("TZ")."\n";



// Now Change the Timezone


putenv("TZ=Australia/Adelaide");



// Show the time


echo "Date ".date("d/m/Y H:i:s",$timestamp)." in ".getenv ("TZ")."\n";




yylu@enreach.com
10-Aug-1999 07:05
Any dates before 12/14/1902 or after 1/18/2038 will be returned as 12/31/1969.


eric.swanson@unh.edu
01-Oct-1999 09:40
Use the zero-filled month and day to store the current year,month,day to a Mysql table.

Just in case somebody makes the same stupid mistake that I did: make SURE to use the zero-fill versions (d,m) and not the other ones (j,n). I accidentally used j for day, performed a checkdate (which passed), and stored the date for October First, 1999 as "1999-10-1", which resolved to "000-00-00" once it hit the database.



deeker@1010software.com
01-Dec-1999 10:47
I was having problems converting a string date to a storable date. If anyone's interested, here's how I did it:


$newsplit = explode("/", $mydate);


$gooddate = date("Y-m-d",mktime(0,0,0,$newsplit[0],$newsplit[1],$newsplit[2]));


Hope that helps someone.



waller@edgeglobal.com
14-Jan-2000 06:40
I don't if this will help anyone, but to insert into a date field using a mysql database and date(), eregi_replace("\-", $data[date]).


If you have an array with a date field, you can use the following:


list($year,$month,$day)=split("\-",$data[date]");


$date = $year.$month.$day;


I believe you can also directly pipe the array-variable to the date() function directly.



ashley@seapagan.org
29-Jan-2000 02:42
Watch out for Daylight Savings Time/Summer Time! If you're using date() with mktime() to count days, I recommend passing midday rather than midnight to mktime().


siggma@siggma.net
03-Feb-2000 09:21
What about the week of the year? I'll just use <?floor(date("z")/7)+1?> for now...


ddwyer@ncic.com
08-Feb-2000 05:56
Safety Tip:

If you want to only do manipulations on time, do not use this function, because it will adjust to local time. Like:


$thetime=date("H:i:s",86400);



...where 86400 is the number of seconds in a day. If you expect 1/2/70 12:00am, you will not be correct unless you live in the UK.


The correct function here will be:

gmdate("H:i:s",86400);



akira@zerodegree.de
14-Feb-2000 12:01
Don't know if it might be a problem for somebody to get a formated d-m-Y out of a timestamp .)


$dates="´20000214081005";
echo date("d-m-Y",time($dates));


Will return you a nicly formated timestamp .) cya



roedelm@letu.edu
03-Mar-2000 03:00
Notation in the manual for date(z) is potentially a little confusing to someone who isn't used to counting like a programmer.

For the record, date(z) for January 1st will return 0, January 2nd will return 1, and so on.



heiko@individual-web.com
05-Mar-2000 09:57
Here is a more accurate way to get the week of the year:


function weekofyear() {

$d = 1;

while
(date("w",mktime(0,0,0,1,$d,date("Y"))) != 1) {

$d++;

};

return(floor((date("z")-$d+13)/7.0-1));

}




jkh@zybermail.dk
08-Mar-2000 04:05
I had some trouble with the date("t") that was supposed to return the number of days in this current month. But it didn't work with my version of PHP, I had to come up with another solution - here is is (and yes it is seen before on this list!) :


$days_in_month = date("j",
mktime(0,0,0,date("n")+1, 1, date("Y"))-1);




unconed@projectx.mx.dk
31-Mar-2000 08:37
To get yesterdays date use:

date("<your format string>",time() - 86400);

(There are 86400 seconds in one day: 24 * 60 * 60)

For tomorrow use +, and for X days in the future/past simply use X times 86400.



bunny@bytech.fi
04-Apr-2000 01:32
I implemented the "week of the year"-script in a rather different way, partly because here in Finland the week starts from monday:


(It hasn't been optimized in any way, so cut the laughter back there :I)



function weekofyear() {

/* array for switching the week to begin
from monday instead of sunday */

$week=array(6,0,1,2,3,4,5);

$p=$week[date("w",mktime(0,0,0,1,1,date("Y")))];

if($p<4)

$start=1-$p;

else

$start=$p+($p-7);

$today=$week[date("w")];

/* no rounding is needed since we
always get full weeks */

return(((date("z")-$today+1)-$start)/7+1);}





alexphpdoc@blamey.freeserve.co.uk
17-Apr-2000 07:03
There is another undocumented format code - B.
This returns the beats into the day, which ranges
from 0 to 999.

i.e. :

beats = seconds_into_day * 1000 / (24*60*60)



mirba@hotmail.com
27-Apr-2000 11:02
Inserting dates into ODBC databases:


I set the field in which insert the date as a char (it doesn't create trouble). I used Todd's code to start with:


$today = date(" j, g, Y");
PRINT "Today is the $today
";
To insert it I use odbc_exec:

$save = odbc_exec ($conn , 'insert into date_query(date_field) value ('$today')');



dave@e-underground.net
29-Jun-2000 03:41
$date = date("m.d.y");
print $date;

That'd spit out 06.28.00 (for today's date).



david@computinginsights.com
02-Jul-2000 02:00
I just used one of the functions listed above to generate an expiration date in my code. I have a form that submits customer requested data to a .php page. The PHP code outputs the current date then adds 90 days to it and outputs the expiration date. Here is the code:


$today = date("l M dS Y");


$expire = date("Y m d",time() + 7776000);


$showexp = date("l M dS Y",time() + 7776000);



I am using two different variables for the expiration because one ($expire) is written to a MySQL DB and the other ($showexp) is for display to the customer in a better format.



rob@OhReally.com
07-Jul-2000 11:09
Dates are compared field by field. So if I do

if ($onedate < $otherdate) { ... }
on a date("d-m-Y") and the first dayfield is smaller than the second, PHP will not look any further. Using a date("Y-m-d") solves this.



rob@OhReally.com
13-Jul-2000 12:37
The client's date can only be used if your visitor would submit this first somehow. PHP is a server-side language, which means the page is parsed before it is sent to the browser. JavaScript is client-side, which means the scripts run on the visitor's pc.


crowley@thegatesofhell.org
19-Jul-2000 03:12
I have been having some problems taking a date entry from a MySQL database (YYYY-MM-DD) and using the date() command to format.



I wasnt able to just insert the query output into the date() command because it kept defaulting to 1st jan 1970. The only way I was able to do it was with the following.



$date=$query_disp[date];

$year=substr($date,0,4);

$month=substr($date,5,2);

$day=substr($date,8,2);

$fdate = date("jS F Y",mktime (0,0,0,$month,$day,$year));





paco@velocity.net
20-Jul-2000 05:50
To get date() to work with a mysql timestamp, wrap it in UNIX_TIMESTAMP()
ie SELECT UNIX_TIMESTAMP(datefield)from blah



omar@programmer.net
22-Jul-2000 02:01
The format string in the date function interprets "de" as a command, when only "d" is a command and "e" is not interpreted. I want to put a spanish word which means "of" and cannot.


tom@subblue.com
24-Jul-2000 12:20
SOLUTION: Calculate the day difference between 2 dates


A few people have been asking about this so i'll share my solution:


I have some dates stored in a MySQL database in the format 'yyyy-mm-dd'


I wanted to find the difference between those dates and the current date in whole days.

Note: you could calculate the difference in months just as easily.


// Routine to display the difference between two dates


// Date stored in the MySQL database as yyyy-mm-dd


$today=date("Y-m-d 00:00");

$date1=strtotime( "2000-07-22 00:00" );


// The date value [2000-07-23] here would be the retrieved mysql string


$date2=strtotime($today);

echo "Difference: ".(($date2-$date1)/86400);


// Prints number of days difference



Other notes added to the PHP manual have helped me a lot, so i thought i'd do my part :-)



int@hotpop.com
25-Jul-2000 05:09
To duplicate the unix date command:

date("D M j g:i:s T Y")



steve@hassard.net
26-Jul-2000 01:56
to convert a unix timestamp into a PHP timestamp do the following:
$mysqlTimestamp = date( 'YmdHis', $myUNIXtimestamp );



robkar97@astud.chalmers.se
31-Jul-2000 04:18
Probably very obvious but for a newbie like myself:


<pre>$today = date("Ymd");</pre>


Produces: 20000731



rob@OhReally.com
03-Aug-2000 01:07
I've seen some people use PHP to format a date which was retrieved from a MySQL-database. Why? Just query the db like this: "SELECT DATE_FORMAT(thefield, '%d-%m-%Y') FROM thetable". The date is returned formatted. Check http://mysql.com/Manual_chapter/manual_Reference.html#Date_and_time_functions for more info.


php@theprojects.org
11-Aug-2000 04:18
when your in PHP SAFE MODE and want to change your timezone, you should do this :

putenv("PHP_TZ=Australia/Adelaide");

as opposed to this :

putenv("TZ=Australia/Adelaide");

for a list of timezones, i have a list here :
http://www.theprojects.org/dev/zone.txt
(sorry, i can't find original list location)

Philip




robert.jackson@utoronto.ca
14-Aug-2000 11:02
I had to make a MySQL query for a given week (i.e. SELECT...WHERE date>='$last_sunday' AND date<='$next_saturday').

The php code I came up with is illustrated by:

$yr="2000";
$mnth="03";
$dy="02";
echo "base date: ".$yr."-".$mnth."-".$dy."&lt;br>";
echo "last sunday: ".date("Y-m-d", mktime(0,0,0,$mnth,$dy-date("w",mktime(0,0,0,$mnth,$dy,$yr)),$yr))."&lt;br>";
echo "next saturday: ".date("Y-m-d", mktime(0,0,0,$mnth,$dy+6-date("w",mktime(0,0,0,$mnth,$dy,$yr)),$yr))."&lt;br>";

This seems to work within a month, across months (including leap years), and across years.



 About Notes


Previous page
 checkdate
 Updated
Sat, 12 Aug 2000
getdate 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.