PHP/Date/date
Версия от 10:37, 26 мая 2010; (обсуждение)
Содержание
- 1 calculate number days from date
- 2 Converting Human-Readable Dates Into Unix Timestamps Using strtotime()
- 3 Determining Whether a Given Year Is a Leap Year
- 4 Displaying Human-Readable Dates and Times
- 5 Format Codes for Use with date()
- 6 Format date in an array
- 7 Formatting a Date with date()
- 8 Formatting Characters for the date() Function for Complete Date and Time
- 9 Formatting Characters for the date() Function for Day
- 10 Formatting Characters for the date() Function for Hour
- 11 Formatting Characters for the date() Function for Minute
- 12 Formatting Characters for the date() Function for Month
- 13 Formatting Characters for the date() Function for Second
- 14 Formatting Characters for the date() Function for Year
- 15 Get different part of a date
- 16 Get file name, size, last access time and modified time
- 17 Greetings based on time
- 18 Making the date and time appear like we expect
- 19 number days any month
- 20 number days current month
- 21 Offsets from UTC
- 22 Outputs the date in the format of 31st of August 2005
- 23 Parsing a date with substr()
- 24 Setting Time Zones and GMT/UTC
- 25 string date ( string date_format [, int timestamp] )
- 26 Using various formatting strings with date().
calculate number days from date
<?php
$futuredate = strtotime("45 days");
echo date("F d, Y", $futuredate);
?>
Converting Human-Readable Dates Into Unix Timestamps Using strtotime()
<?php
$mydatestrings = array("now", "today", "tomorrow", "yesterday",
"Thursday", "this Thursday", "last Thursday", "+2 hours", "-1 month", "+10 minutes",
"30 seconds", "+2 years -1 month", "next week","last month", "last year", "2 weeks ago"
);
foreach($mydatestrings as $mydate)
echo "$mydate:" . date("r", strtotime($mydate)) ;
?>
Determining Whether a Given Year Is a Leap Year
<?php
function is_leap_year($year)
{
$ts = strtotime("$year-01-01-");
return date("L", $ts);
}
for($i = 2000; $i <= 2010; $i++)
{
$output = "$i is ";
if( !is_leap_year($i) )
$output .= "not ";
$output .= "a leap year.<br />\n";
echo $output;
}
?>
Displaying Human-Readable Dates and Times
<?php
$time = time();
$formats = array(
"U",
"r",
"c",
"l, F jS, Y, g:i A",
"H:i:s D d M y",
"m/j/y g:i:s a O (T)"
);
foreach($formats as $format)
echo "<p><b>$format</b>: " . date($format) . "</p>\n";
?>
Format Codes for Use with date()
Format Description Example
a am or pm lowercase pm
A AM or PM uppercase PM
B Swatch beat (timezone-free "Internet time") 771
d Day of month (number with leading zeroes) 08
D Day of week (three letters) Wed
F Month name October
g Hour (12-hour format�no leading zeroes) 6
G Hour (24-hour format�no leading zeroes) 18
h Hour (12-hour format�leading zeroes) 06
H Hour (24-hour format�leading zeroes) 18
i Minutes 31
I Daylight savings time (Boolean value) 1
j Day of the month (no leading zeroes) 8
l Day of the week (name) Wednesday
L Leap year (1 for yes, 0 for no) 0
m Month of year (number�leading zeroes) 10
M Month of year (three letters) Oct
n Month of year (number�no leading zeroes) 10
o Offset in hours from GMT (in [+-]HHMM format) +0100
r Full date standardized to RFC 822 (http://www.faqs.org/rfcs/rfc822.html) Wed, 8 Oct 2003 18:31:15+0100
s Seconds, with leading zeroes 15
S English suffix for date in month (e.g. 20th) th
t Number of days in the given month 31
T Timezone setting on the machine used BST
U Unix timestamp 1065634275
w Day of week (number indexed from Sunday = 0) 3
W Week of year 41
y Year (two digits) 03
Y Year (four digits) 2003
z Day of year (0�366) 280
Z Offset in seconds from GMT 3600
Format date in an array
<?php
$mydates = array("2005-01-01", "2005-01-03", "2005-05-22", "2005-05-23", "2005-12-31");
foreach($mydates as $mydate)
echo date("D d M Y: \w\e\e\k W", strtotime($mydate)) . "<br />\n";
?>
Formatting a Date with date()
<html>
<head>
<title>Formatting a Date with date()</title>
</head>
<body>
<div>
<?php
print date("m/d/y G.i:s", time());
print "<br/>";
print "Today is ";
print date("jS of F Y, \a\\t g.i a", time());
?>
</div>
</body>
</html>
Formatting Characters for the date() Function for Complete Date and Time
Character Description
c ISO-8601 format (YYYY-MM-DDTHH:MM:SS�HHMM, for example, 2005-03-14T19:38:08+10:00).
r RFC-2822 format WWW, DD MMM YYYY HH:MM:SS �HHMM, for example, Mon, 14 Mar 2005 19:38:08 +1000).
U Seconds since the Unix epoch. Calling date("U")with no timestamp argument produces the same output as the time()function.
Formatting Characters for the date() Function for Day
Character Description
d Day of the month, with leading zeros (two digits).
j Day of the month (no leading zeros).
S Ordinal suffix for the day of the month, two characters (st, nd, th); most commonly used in combination with j.
l(lowercase L) Full name of the day of the week (Monday, Tuesday, and so on).
D A textual representation of a day, three letters (Mon, Tue, and so on).
w Numeric representation of the day of the week (0 = Sunday, 6 = Saturday).
Formatting Characters for the date() Function for Hour
Character Description
h Hour in 12-hour format, with leading zero (two digits).
g Hour in 12-hour format (no leading zero).
H Hour in 24-hour format, with leading zero (two digits).
G Hour in 24-hour format (no leading zero).
a am/pm (lowercase).
A AM/PM (uppercase).
O(uppercase o) String representation of the difference in hours between local time and GMT/UTC (for example, +1000, �0500).
Formatting Characters for the date() Function for Minute
Character Description
i Minute, with leading zero (two digits).
j Minute (no leading zero).
Formatting Characters for the date() Function for Month
Character Description
F Full name of the month (January, February, and so on).
M Three-letter abbreviation for the month (Jan, Feb, and so on).
m Numeric representation for the month, with leading zero (two digits).
n Numeric representation for the month (no leading zero).
Formatting Characters for the date() Function for Second
Character Description
s Second, with leading zero (two digits).
Z Integer representation of the difference in seconds between local time and GMT/UTC (for example, 36000 for GMT+1000 and �18000 for GMT�0500).
Formatting Characters for the date() Function for Year
Character Description
y Two-digit year.
Y Four-digit year.
Get different part of a date
$stamp = mktime(19,45,0,10,20,2004);
print "Today is day ".date("d",$stamp)." of ".date("F",$stamp)." and day ".(date("z",$stamp)+1);
print " of the year ".date("Y",$stamp).". The time is ".date("h:i A",$stamp);
print " (also known as ".date("H:i",$stamp).").";
Get file name, size, last access time and modified time
<?php
function tstamp_to_date($tstamp) {
return date("m-d-y g:i:sa", $tstamp);
}
$file = "/data.txt";
$fh = fopen($file, "r");
$fileinfo = fstat($fh);
echo "Filename: ".basename($file)."<br />";
echo "Filesize: ".round(($fileinfo["size"]/1024), 2)." kb <br />";
echo "Last accessed: ".tstamp_to_date($fileinfo["atime"])."<br />";
echo "Last modified: ".tstamp_to_date($fileinfo["mtime"])."<br />";
?>
Greetings based on time
<html>
<head>
<title>Greetings</title>
</head>
<body>
<?php
$hour = date( "G" );
$now = date( "g:i a" );
$msg = "Good Evening.";
if( $hour < 18 ) { $msg = "Good Afternoon."; }
if( $hour < 12 ) { $msg = "Good Morning."; }
echo( "$msg The time is $now" );
?>
</body>
</html>
Making the date and time appear like we expect
<?php
$timestamp= time( );
echo date("m/d/y G.i:s",$timestamp);
?>
number days any month
<?php
$lastday = mktime(0, 0, 0, 3, 0, 2006);
printf("There are %d days in February, 2006.", date("t",$lastday));
?>
number days current month
<?php
printf("There are %d days in %s.", date("t"), date("F"));
?>
Offsets from UTC
$pc_timezones = array(
"GMT" => 0, // Greenwich Mean
"UTC" => 0, // Universal (Coordinated)
"WET" => 0, // Western European
"WAT" => -1*3600, // West Africa
"AT" => -2*3600, // Azores
"NFT" => -3*3600-1800, // Newfoundland
"AST" => -4*3600, // Atlantic Standard
"EST" => -5*3600, // Eastern Standard
"CST" => -6*3600, // Central Standard
"MST" => -7*3600, // Mountain Standard
"PST" => -8*3600, // Pacific Standard
"YST" => -9*3600, // Yukon Standard
"HST" => -10*3600, // Hawaii Standard
"CAT" => -10*3600, // Central Alaska
"AHST" => -10*3600, // Alaska-Hawaii Standard
"NT" => -11*3600, // Nome
"IDLW" => -12*3600, // International Date Line West
"CET" => +1*3600, // Central European
"MET" => +1*3600, // Middle European
"MEWT" => +1*3600, // Middle European Winter
"SWT" => +1*3600, // Swedish Winter
"FWT" => +1*3600, // French Winter
"EET" => +2*3600, // Eastern Europe, USSR Zone 1
"BT" => +3*3600, // Baghdad, USSR Zone 2
"IT" => +3*3600+1800, // Iran
"ZP4" => +4*3600, // USSR Zone 3
"ZP5" => +5*3600, // USSR Zone 4
"IST" => +5*3600+1800, // Indian Standard
"ZP6" => +6*3600, // USSR Zone 5
"SST" => +7*3600, // South Sumatra, USSR Zone 6
"WAST" => +7*3600, // West Australian Standard
"JT" => +7*3600+1800, // Java
"CCT" => +8*3600, // China Coast, USSR Zone 7
"JST" => +9*3600, // Japan Standard, USSR Zone 8
"CAST" => +9*3600+1800, // Central Australian Standard
"EAST" => +10*3600, // Eastern Australian Standard
"GST" => +10*3600, // Guam Standard, USSR Zone 9
"NZT" => +12*3600, // New Zealand
"NZST" => +12*3600, // New Zealand Standard
"IDLE" => +12*3600 // International Date Line East
);
Outputs the date in the format of 31st of August 2005
print date("jS of F Y");
Parsing a date with substr()
$date = "2010-12-03 05:12:56";
$date_parts[0] = substr($date,0,4);
$date_parts[1] = substr($date,5,2);
$date_parts[2] = substr($date,8,2);
$date_parts[3] = substr($date,11,2);
$date_parts[4] = substr($date,14,2);
$date_parts[5] = substr($date,17,2);
?>
Setting Time Zones and GMT/UTC
<?php
$ts = time();
echo date("r", $ts) . "<br />\n";
echo date("r", $ts) . "<br />\n";
echo strftime("%D %T %Z", $ts) . "<br />\n";
?>
string date ( string date_format [, int timestamp] )
Format characters for use in date( )
Format character Description Example
a Lowercase am/pm am or pm
A Uppercase am/pm AM or PM
B Swatch Internet Time 000 to 999
c ISO 8601 date, time, and time zone 2004-06-18T09:26:55+01:00
d 2-digit day of month, leading zeros 01 to 31
D Day string, three letters Mon, Thu, Sat
F Month string, full January, August
g 12-hour clock hour, no leading zeros 1 to 12
G 24-hour clock hour, no leading zeros 0 to 23
h 12-hour clock hour, leading zeros 01 to 12
H 24-hour clock hour, leading zeros 00 to 23
i Minutes with leading zeros 00 to 59
I Is daylight savings time active? 1 if yes, 0 if no
j Day of month, no leading zeros 1 to 31
l Day string, full Monday, Saturday
L Is it a leap year? 1 if yes, 0 if no
m Numeric month, leading zeros 01 to 12
M Short month string Jan, Aug
n Numeric month, no leading zeros 1 to 12
O Difference from GMT 200
r RFC-822 formatted date Sat, 22 Dec 1979 17:30 +0000
s Seconds, with leading zeros 00 to 59
S English ordinal suffix for day number st, nd, rd, or th
t Number of days in month 28 to 31
T Time zone for server GMT, CET, EST
U Unix Timestamp 1056150334
w Numeric day of week 0 (Sunday), 6 (Saturday)
W ISO-8601 week number of year 30 (30th week of the year)
y Two-digit representation of year 97, 02
Y Four-digit representation of year 1997, 2002
z Day of year 0 to 366
Z Time zone offset in seconds -43200 to 43200
Using various formatting strings with date().
<?php
$time = time();
$formats = array("U","r","c","l, F jS, Y, g:i A","H:i:s D d M y","m/j/y g:i:s O (T)" );
foreach($formats as $format)
echo "<p><b>$format</b>: " . date($format, $time) . "</p>\n";
?>