PHP/Date/strftime

Материал из Web эксперт
Версия от 10:03, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Checking for the day of the week

   <source lang="html4strict">

<?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!";

}

 </source>
   
  


Finding days of the week, month, and year

   <source lang="html4strict">

<?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."; ?>

 </source>
   
  


Finding the current date and time

   <source lang="html4strict">

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

 </source>
   
  


Format Characters Used by the strftime() Function for Day

   <source lang="html4strict">

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.

 </source>
   
  


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

   <source lang="html4strict">

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

 </source>
   
  


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

   <source lang="html4strict">

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.

 </source>
   
  


Format Characters Used by the strftime() Function for Hour

   <source lang="html4strict">

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

 </source>
   
  


Format Characters Used by the strftime() Function for Minute

   <source lang="html4strict">

Character Description %M Minute.

 </source>
   
  


Format Characters Used by the strftime() Function for Month

   <source lang="html4strict">

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

 </source>
   
  


Format Characters Used by the strftime() Function for Second

   <source lang="html4strict">

Character Description %S Second.

 </source>
   
  


Format Characters Used by the strftime() Function for Week

   <source lang="html4strict">

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).

 </source>
   
  


Format Characters Used by the strftime() Function for Year

   <source lang="html4strict">

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.

 </source>
   
  


Generating the days in a month

   <source lang="html4strict">

<?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;

} ?>

 </source>
   
  


Generating the days in a week

   <source lang="html4strict">

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

} ?>

 </source>
   
  


Printing a formatted date string with strftime()

   <source lang="html4strict">

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

 </source>
   
  


Printing a formatted time string with other text

   <source lang="html4strict">

<? 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"); ?>

 </source>
   
  


Printing a greeting or printing a form

   <source lang="html4strict">

<?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">
<input type="submit" value="Say Hello"> </form> _HTML_; } ?>

 </source>
   
  


Skip ahead one day to the Tuesday after the first Monday

   <source lang="html4strict">

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

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

 </source>
   
  


strftime.php

   <source lang="html4strict">

<?php

  setlocale(LC_ALL, "it_IT");
  $tickets = 2;
  $departure_time = 1238837700;
  $return_time = 1239457800;
  $cost = 1350.99;

?> <?php echo $tickets; ?>
<?php echo strftime("%d %B, %Y", $departure_time); ?>
<?php echo strftime("%d %B, %Y", $return_time); ?>

 </source>