Delicious Bookmark this on Delicious Share on Facebook SlashdotSlashdot It! Digg! Digg

PHP : Function Reference : Date and Time Functions : strftime


Format a local time/date according to locale settings (PHP 4, PHP 5)
string strftime ( string format [, int timestamp] )

Example 460. strftime() locale examples

(LC_TIME, "C");
setlocale(LC_TIME, "fi_FI");
strftime(" in Finnish is %A,");
setlocale(LC_TIME, "fr_FR");
strftime(" in French %A and");
setlocale(LC_TIME, "de_DE");
strftime(" in German %A.\n");

Example 461. ISO 8601:1988 week number example

/*     December 2002 / January 2003
ISOWk  M   Tu  W   Thu F   Sa  Su
----- ----------------------------
51     16  17  18  19  20  21  22
52     23  24  25  26  27  28  29
1      30  31   1   2   3   4   5
2       6   7   8   9  10  11  12
3      13  14  15  16  17  18  19   */

// Outputs: 12/28/2002 - %V,%G,%Y = 52,2002,2002
echo "12/28/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/28/2002")) . "\n";

// Outputs: 12/30/2002 - %V,%G,%Y = 1,2003,2002
echo "12/30/2002 - %V,%G,%Y = " . strftime("%V,%G,%Y", strtotime("12/30/2002")) . "\n";

// Outputs: 1/3/2003 - %V,%G,%Y = 1,2003,2003
echo "1/3/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2003")) . "\n";

// Outputs: 1/10/2003 - %V,%G,%Y = 2,2003,2003
echo "1/10/2003 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/10/2003")) . "\n";

/*     December 2004 / January 2005
ISOWk  M   Tu  W   Thu F   Sa  Su
----- ----------------------------
51     13  14  15  16  17  18  19
52     20  21  22  23  24  25  26
53     27  28  29  30  31   1   2
1       3   4   5   6   7   8   9
2      10  11  12  13  14  15  16   */

// Outputs: 12/23/2004 - %V,%G,%Y = 52,2004,2004
echo "12/23/2004 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/23/2004")) . "\n";

// Outputs: 12/31/2004 - %V,%G,%Y = 53,2004,2004
echo "12/31/2004 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("12/31/2004")) . "\n";

// Outputs: 1/2/2005 - %V,%G,%Y = 53,2004,2005
echo "1/2/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/2/2005")) . "\n";

// Outputs: 1/3/2005 - %V,%G,%Y = 1,2005,2005
echo "1/3/2005 - %V,%G,%Y = " . strftime("%V,%G,%Y",strtotime("1/3/2005")) . "\n";


Related Examples ( Source code ) » strftime

Code Examples / Notes » strftime


When using strftime to generate time stamps for inclusion in  RSS feeds, be sure to use %z for the timezone (RFC-822 format) rather than %Z -- for some reason, %Z fails to validate.

jon keating

Under windows if you are using Japanese version, you must use the following code:
setlocale(LC_ALL, "Japanese_Japan.20932") for EUC
setlocale(LC_ALL, "Japanese_Japan.932") for SJIS
I found the following page that helped me with this issue:


To partialy correct Neo's on RFC 850 date...
RFC 850 is obsolete by RFC 1036.
In HTTP header, RFC 1123 is the first choice: it has a fixed length format and 4 digits year.
Therefore, the correct format is:
gmstrftime ("%a, %d %b %Y %T %Z", time ());
Output example: Sun, 06 Nov 1994 08:49:37 GMT
[red. Use date(DATE_RFC850, time()); instead. (Or DATE_RFC1123 or DATE_RFC1036)]


To mcallister, phloe and anyone else who want to get on the getDaySuffix() band wagon:
There are already functions to do this. All you really have to do is:
<?=date("jS", mktime(0,0,0,1,$myDay,2006))?>
Obviously, the only part of mktime that really matters is $myDay.


To get a RFC 850 date (used in HTTP) of the current time:
gmstrftime ("%A %d-%b-%y %T %Z", time ());
This will get for example:
Friday 25-Jun-04 03:30:23 GMT
Please note that times in HTTP-headers _must_ be GMT, so use gmstrftime() instead of strftime().


To correct an error in the above list of formatting codes:
%p - either `AM' or `PM' according to the given time value, or the corresponding strings for the current locale (note result is in capitals)
%P - either `am' or `pm' according to the given time value, or the corresponding strings for the current locale (note result is in lower case)
In addition, the following codes seem to return a value on my system (Linux, Apache/1.3.33, PHP/4.4.1), but are not documented above.  I have included the result given by passing them to gmstrftime() with a timestamp of 0 (unix epoch).  gmstrftime() was used to avoid timezone/DST differences.
%F: "1970-01-01" (appears to be an SQL-formatted version of the date)
%k: "0" (seems to be the hour in 24-hour clock, without leading zeros (space-padded))
%l: "12" (seems to be the hour in 12-hour clock, without leading zeros (space-padded))
%s: "-3600"
%s seems to be the Unix timestamp passed to the function, but somehow based on the current locale/TZ settings (even in gmstrftime()).  
On my system strftime("%H:%M:%S", 0) returns "01:00:00", and strftime("%s", 0) returns "0".  Using gmstrftime() I get "00:00:00" and "-3600" respectively.

ben dot holland

This little function allows you to provide a reasonably human readable string and convert to a timestamp - see example in comments below.
I find it far more useful than having to remember all the '%' modifiers. Am also well aware of its failings but it works in a lot of the real life situations I've come across.
function AmazingStringFromTime($str, $nTimestamp = null)
   // This function reads a human readable string representation of dates. e.g.
   // DD MM YYYY => 01 07 1978
   // DDD D MMM YY => Mon 1 Jul 78
   $arrPairs = array(
   "DDDD"  => "%A",
   "DDD"   => "%a",
   "DD"    => "%d",
   "D"     => "%e", // has leading space: ' 1', ' 2', etc for single digit days
   "MMMM"  => "%B",
   "MMM"   => "%b",
   "MM"    => "%m",
   "YYYY"  => "%Y",
   "YY"    => "%y",
   "HH"    => "%H",
   "hh"    => "%I",
   "mm"    => "%M",
   "ss"    => "%S",
   $str = str_replace(array_keys($arrPairs), array_values($arrPairs), $str);
   return strftime($str, $nTimestamp);


This function is useful for news posts.
It displays the creation date and elapsed time like:
Sunday, December 4, 2005 - 2 days ago
Monday, December 5, 2005 - Yesterday
Monday, November 28, 2005 - 1 week ago
Friday, October 28, 2005 - 6 weeks ago
function off($date){
$date = strtotime($date);
$offset = (strftime("%j")+strftime("%Y")*365)-
if ($offset>7){
$offset = (strftime("%V")+strftime("%Y")*52)-
$end=($offset!=0?($offset>1?$offset . " weeks ago":"a week ago"):"Today");
} else
$end=($offset!=0?($offset>1?"$offset days ago":"Yesterday"):"Today");
return strftime("%A, %B %e, %Y",$date)." - ". $end;
To insert the time:


The locale for dutch on a win2k computer is not "nl_NL"
but "dutch".


The following function implements the conversion specifiers which are not supported on Win32 platforms:
(Note: the specifiers %V, %G and %g can be implemented using other functions described in this section)
function strftime_win32($format, $ts = null) {
if (!$ts) $ts = time();
$mapping = array(
'%C' => sprintf("%02d", date("Y", $ts) / 100),
'%D' => '%m/%d/%y',
'%e' => sprintf("%' 2d", date("j", $ts)),
'%h' => '%b',
'%n' => "\n",
'%r' => date("h:i:s", $ts) . " %p",
'%R' => date("H:i", $ts),
'%t' => "\t",
'%T' => '%H:%M:%S',
'%u' => ($w = date("w", $ts)) ? $w : 7
$format = str_replace(
return strftime($format, $ts);


Thats how I get the %G, %g ISO Year and %V Week with php 4.4.4 on Window$.
It works quite simple:
1. on January it can't be a week bigger than 51, thats what i've learned in school
2. on december many weeks have gone since the last New Year's Eve and therefore it cant be the first week of the year
i hope it saves time
function YearOfWeek($format,$date = NULL){
       $date = time();
   list ($G,$V,$month) = explode('_',date ('Y_W_m',$date));
   if( ($month === '01') && ($V >= 52 ) ){
   }else  if( ($month === '12') && ($V === '01') ){
   $g = substr($G, -2, 2);
   $format = str_replace(array('%V','%G','%g'),array($V,$G,$g),$format);
   return ($format);
   $format = 'week %V of the year %G (%g), year %Y (%y)';
   $date =  mktime(0, 0, 0, 12, 31, 2008);
//week 01 of the year 2009 (09), year 2008 (08)
   echo strftime(YearOfWeek($format,$date), $date) . "\n";
   $date =  mktime(0, 0, 0, 1, 1, 2010);
//week 53 of the year 2009 (09), year 2010 (10)
   echo strftime(YearOfWeek($format,$date), $date) . "\n";
   echo strftime(YearOfWeek($format)) . "\n";
//something like: week 51 of the year 2006 (06), year 2006 (06)
//depends on the day you do this


Solaris 2.6 and 7 define the
%u specifier differently than noted here. Day 1 is Sunday, not Monday. Solaris 8 gets it right.

judas dot iscariote

Security notice:
You can end with a (probably unexpected) XSS if the $format parameter is user submitted data ;)
echo strftime("<script>alert('lol');</script>");
PHP does not check if $format is a valid format, so the usual security precautions should be taken here .


Searching for translation from IBASE-Timestamp to EU-Dateformat DD.MM.YYYY
Maybe it's useful for somebody ;-)

arpad borsos

Please note that the strftime output is not encoded in utf8 so you have to use utf8_encode() if you want to print localized month names e.g. for German "März"
[red. this depends on the platform though - some Windows locales *do* use UTF-8 for example]


note, that for some languages you MUST set LC_ALL instead of LC_TIME.
note that you further have to explicitly define your output-encoding (default is ISO-8859-1 [which makes problems for some languages])!
at least i expirienced this behaviour on a german WinXP-PHP4 environment:
// does not work - gives question marks:
setlocale(LC_TIME, 'RUS'); // ISO Alpha-3 is supported by xp
echo strftime('%A', time());
// DOES work:
header('Content-Type: text/html; charset=UTF-8'); // you could also use another charset here if iconv isn't installed on your system.
echo setlocale(LC_ALL, 'RUS').': ';
echo iconv('windows-1251', 'UTF-8', strftime('%A', time()))."\n";


Note that setting LC_TIME is not enough for some locales under Windows, e.g. Czech, because there are some characters not contained in default (US) character set like 'è' (c with hook), 'ø' (r with hook).
If you run Apache as regular application and have set your locale to Czech (ControlPanel/RegionalOptions), there is no problem and 'September' is correctly translated as 'záøí', 'Thursday' as 'ètvrtek'.
But if you run Apache as service, you get 'zárí', and 'ctvrtek'.
To get things work as you expect you must set LC_CTYPE beside LC_TIME, or set LC_ALL.
 $locale = 'Czech_Czech.1250';
 $res = setlocale( 'LC_CTYPE', $locale); //important
 $res = setlocale( 'LC_TIME', $locale);
 echo strftime( '%A %m. %B %Y', mktime( 0,0,0,9,6,2001));

dead dot screamer

new address for MSDN function strftime:


Marian Sigler
Your point about javascript not being universal is correct. However, accept language header does not tell you the local time difference, which is the essential problem.
Also, as you point out, accept language is also not universal.
Most browsers do accept javascript. it is in common use by many websites. It is a safe bet if you are using drop down menus for instance that the script will work, otherwise they would not be able to use your site at all.


Locale names are OS dependent. HP-UX 11.0, for example, has three
German locales, de_DE.roman8, de_DE.iso88591, and
The command locale -a will display all available locales on a system.
So on HP-UX, to get German dates:
setlocale("LC_TIME", "de_DE.roman8");


In the strftime override-function of rickenmeer at hotmail dot com there is an error.
The line:
 $year = 1969;
should read:
 $year = 1970;

moritz stoltenburg

In response to the note supplied by "segfault nycap rr com".
Here is a quicker and easier way:
Use the MySQL Time Function TIME_FORMAT() in your query:
mysql> SELECT TIME_FORMAT('14:35:00', '%l:%i %p');
Or try '%l' in PHP, it works on most systems and is part of the Open Group specification of strftime().
echo strftime('%l:%M %p', strtotime('14:35:00'));

josh dot helzer

If strlen($format) >= 1024, the output of strftime($format) is the empty string.


i had to use the czech representation of time on unix machine, running debian and linux version of apache with php 4
for me the best solution was to use this code:
    setlocale(LC_ALL, 'cs_CZ.iso88592');
then you can do everything in czech language with correct iso-8859-2 encoding ;D

jerome dot place

Here is a function to convert dates before 1970, very useful if you are still using php 4 (it is supported in php5) :
# convert a date to special format
# $date is like 2000-01-01 00:00:00
# $format : refer to strftime function
function convert_date($date,$format) {
if($date=='0000-00-00 00:00:00' OR $date=='0000-00-00' OR $date=='' OR $date==NULL) {
return '';
else {
if(phpversion() < 5.0 AND $year < 1970) {

$new_date=substr_replace($date,'1980',0,4); # we replace the year by a year after 1970
$new_format=eregi_replace('%a|%A|%u','',$format); # we remove days information from the format because they would be wrong
$new_date=strftime($new_format,strtotime($new_date)); # we convert the date
$new_date=eregi_replace('1980',$year,$new_date); # we put back the real year
return $new_date;
else {
return strftime($format,strtotime($date));


Function strftime() use the locales installed in your system (linux).
If you are like me and only leave in the system the locales you use normally (en_US and your own language locale, like es_ES), you'll only be able to use the locales installed. If your application is translated to other languages, you need these locales too.
The name of the locale in your system is important too. This can be a problem when you want to distribute the app.
If you have this locales in your system:
and use setlocale('es_ES'), the result will use the iso-8859-1 charset even if you have all your system, files and configuration options in UTF-8. To receive content in UTF-8, in this example, you need to use setlocale('es_ES.UTF-8') or setlocale('es_ES.UTF-8@UTF-8').
The definition of locales can change from one system to another, and so the charset from the results.


For Spanish:
setlocale(LC_ALL, "sp");
echo strftime("%d. %B %Y");


For freebsd user:
You can find the full list of your locale under /usr/share/locale.
For example da_DK.ISO8859-1 under this directory will set up the locale to danish.


Beware of '%D':
the comment shown expects that this is the same as '%m/%d/%y'.
This is wrong: '%D' is only expected to returned an abbreviated numeric date according to the current locale:
In the German locale '%D' is '%y.%m.%d'
In the French locale '%D' is '%d/%m/%y'
The locale rules still apply to %D as with '%A'...
Beware that some C libraries do not support '%D' and/or '%A'  or do not support them accordingly. Using strftime() is then system-dependant, because PHP use the C function provided by the system on which it runs.


As said in these comments, Windows strftime() doesn't support %e. However, to achieve a similar effect (not 100%) you can use %#d. The # flag will remove the leading zero, so you do get single digits, but without the space that would be added by %e in other environments.

nielsvan den berge

A small function to get the first weekday of the month.
For example the first monday of the month, or the first friday, etc.
  *  Gets the first weekday of that month and year
  *  @param  int   The day of the week (0 = sunday, 1 = monday ... , 6 = saturday)
  *  @param  int   The month (if false use the current month)
  *  @param  int   The year (if false use the current year)
  *  @return int   The timestamp of the first day of that month
 function get_first_day($day_number=1, $month=false, $year=false)
   $month  = ($month === false) ? strftime("%m"): $month;
   $year   = ($year === false) ? strftime("%Y"): $year;
   $first_day = 1 + ((7+$day_number - strftime("%w", mktime(0,0,0,$month, 1, $year)))%7);
   return mktime(0,0,0,$month, $first_day, $year);
// this will output the first wednesday of january 2007 (wed 03-01-2007)
echo strftime("%a %d-%m-%Y", get_first_day(3, 1, 2007));


A simple work around for displaying dates in local format is to get the browser to do it for you using javascript.
This routine accepts a date time field from sql,
and gets the timezone offset for your server, and passes these over to the browser in the form of a javascript script.
Javascript calculates the time difference and uses toLocaleString to display it.
function DisplaySQLDateInLocalTime($SQLDate){
list($year,$mon,$day,$hour,$min,$sec)=split('[-: ]',$SQLDate);
$TimeZoneOffset=date('Z') /60;
$JS.="LocalTime = new Date();\n";
$JS.="TimeZoneOffsetInMins = LocalTime.getTimezoneOffset();\n";
$JS.="timediff=-TimeZoneOffsetInMins - ServerTimeZoneOffset;\n";
$JS.="LocalTime = new Date(".$year.","
$JS.="LocalTime=new Date(DateVal+timediff*1000*60);\n";
$JS.="document.write( LocalTime.toLocaleString());\n";
return $JS;

marian sigler

@NicSoft: And what if the user's browser doesn't support JavaScript? You should either use the possibilities offered by PHP (most browsers send an Accept-Language header, from where you can extract the language to use) or at least offer the english name (e.g. inside <noscript>)


(in addition to Andy's post)
To get a RFC 2822 date (used in RSS) of the current local time :
echo strftime ("%a, %d %b %Y %H:%M:%S %z") ;
Note: option %z / %Z - work different on Windows platform, for example
output of this code line can be:
Thu, 02 Nov 2006 09:54:59 Jerusalem Standard Time (on Windows)
Thu, 02 Nov 2006 09:54:59 +0200                   (on Linux)
[red. It is much smarter to use date(DATE_RSS); here]


%k will give you %H (hour, 24-hour clock) with the leading zero replaced by a space.  I have only tested this on one linux system so far, it may not work on windows or other linux builds.

Change Language

Follow Navioo On Twitter
eXTReMe Tracker