PHP/Date/strftime

Материал из Web эксперт
Перейти к: навигация, поиск

Checking for the day of the week

 
<?php
if (1 == date("w")) {
    print "Welcome to the beginning of your work week.";
}
if (1 == strftime("%w")) {
    print "Only 4 more days until the weekend!";
}



Finding days of the week, month, and year

 
<?php
print strftime("Today is day %d of the month and %j of the year.");
print "Today is day ".date("d")." of the month and ".date("z")." of the year.";
?>



Finding the current date and time

 
<?php
print strftime("%c");
print "\n";
print date("r");
?>



Format Characters Used by the strftime() Function for Day

 
Character Description 
%A        Full weekday name. 
%a        Abbreviated weekday name. 
%u        Weekday number (1 = Monday, 7 = Saturday). 
%d        Day of the month, with leading zero. 
%e        Day of the month, with leading space. 
%j        Day of the year (001�366). Note that numbering begins with 1 and not 0.



Format Characters Used by the strftime() Function for Formatting Characters

 
Character Description 
%n        New line. 
%t        Tab. 
%%        The percent character.



Format Characters Used by the strftime() Function for Full Date and/or Time

 
Character Description 
%c        Preferred date and time representation for the current locale. 
%D        Current date; equivalent to %m/%d/%y. 
%p        a.m./p.m. indicator. 
%R        Time in 24-hour notation. 
%r        Time in 12-hour (am/pm) notation. 
%T        Current time; equivalent to %H:%M:%S. 
%x        Preferred date representation. 
%X        Preferred time representation. 
%z or %Z  Time zone.



Format Characters Used by the strftime() Function for Hour

 
Character Description 
%H        Hour (00�23). 
%I        Hour (01�12)



Format Characters Used by the strftime() Function for Minute

 
Character Description 
%M        Minute.



Format Characters Used by the strftime() Function for Month

 
Character Description 
%B        Full name of the month. 
%b or %h  Abbreviated name of the month. 
%m        Number of the month, with leading zero.



Format Characters Used by the strftime() Function for Second

 
Character Description 
%S        Second.



Format Characters Used by the strftime() Function for Week

 
Character Description 
%U        Number of the week of the year, with Sunday as the first day of the week. 
%V        ISO-8601 number of the week of the year, with Monday as the first day of the week (01�53). 
%W        Number of the week of the year, with Monday as the first day of the week (decimal number).



Format Characters Used by the strftime() Function for Year

 
Character Description 
%g        Two-digit year for ISO-8601 week of the year. 
%G        Four-digit year for ISO-8601 week of the year. 
%y        Two-digit year. 
%Y        Four-digit year.



Generating the days in a month

 
<?php
$now = time();
if (3 < strftime("%H", $now)) { $now += 7200; }
$this_month = strftime("%m",$now);
$day = mktime(0,0,0,$this_month,1);
$month_end = mktime(0,0,0,$this_month+1,1);
while ($day < $month_end) {
  print strftime("%c",$day); 
  $day += 86400;
}
?>



Generating the days in a week

 
<?php
$now = time();
if (3 < strftime("%H", $now)) { $now += 7200; }
$today = strftime("%w", $now);
$start_day = $now - (86400 * $today);
for ($i = 0; $i < 7; $i++) {
  print strftime("%c",$start_day + 86400 * $i);
}
?>



Printing a formatted date string with strftime()

 
<?
print strftime("%m/%d/%y");
?>



Printing a formatted time string with other text

 
<?
print "strftime() says: ";
print strftime("Today is %m/%d/%y and the time is %I:%M:%S");
print "\n";
print "date() says: ";
print "Today is " . date("m/d/y") . " and the time is " . date("h:i:s");
?>



Printing a greeting or printing a form

 
<?php
if ($_POST["user"]) {
    print "Hello, ";
    print $_POST["user"];
    print "!";
} else {
    print <<<_HTML_
<form method="post" action="$_SERVER[PHP_SELF]">
Your Name: <input type="text" name="user">
<br/>
<input type="submit" value="Say Hello">
</form>
_HTML_;
}
?>



Skip ahead one day to the Tuesday after the first Monday

 
<?
$monday = strtotime("Monday", $november);

$election_day = strtotime("+1 day", $monday);
print strftime("Election day is %A, %B %d, %Y", $election_day);
?>



strftime.php

 
<?php
   setlocale(LC_ALL, "it_IT");
   $tickets = 2;
   $departure_time = 1238837700;
   $return_time = 1239457800;
   $cost = 1350.99;
?>
<?php echo $tickets; ?><br />
<?php echo strftime("%d %B, %Y", $departure_time); ?><br />
<?php echo strftime("%d %B, %Y", $return_time); ?><br />