tailieunhanh - Giải pháp thiết kế web động với PHP - p 44
FORMATTING TEXT AND DATES Format character Description %m %D %d %a * %H %h %I %i %S %s %R %r %% Months, no leading zero Days with leading zero Days, no leading zero Total number of days Hours with leading zero Hours, no leading zero Minutes with leading zero Minutes, no leading zero Seconds with leading zero Seconds, no leading zero Display minus when negative, plus when positive Display minus when negative, no sign when positive Percentage sign Examples 2, 11 03, 24 3, 24 15, 231 03, 23 3, 23 05, 59 5, 59 05, 59 5, 59 -, + % *. | FORMATTING TEXT AND DATES Format character Description Examples m Months no leading zero 2 11 D Days with leading zero 03 24 d Days no leading zero 3 24 a Total number of days 15 231 H Hours with leading zero 03 23 h Hours no leading zero 3 23 I Minutes with leading zero 05 59 i Minutes no leading zero 5 59 S Seconds with leading zero 05 59 s Seconds no leading zero 5 59 R Display minus when negative plus when positive - r Display minus when negative no sign when positive - Percentage sign A bug verified in PHP produces an incorrect result for the total number of days on Windows. Hopefully this will be fixed in a subsequent release. The following example in shows how to get the difference between the current date and the American Declaration of Independence using diff and displaying the result with the format method p php independence new DateTime 7 4 1776 now new DateTime interval now- diff independence echo interval- format Y years m months d days since American independence. p If you load into a browser you should see something similar to the following screenshot of course the actual period will be different . 411 CHAPTER 14 Download from Wow eBook The format characters follow a logical pattern. Uppercase characters always produce at least two digits with a leading zero if necessary. Lowercase characters have no leading zero. What might not be immediately obvious is that with the exception of a which represents the total number of days the format characters represent only specific parts of the overall interval. For example if you change the format string to interval- format m months it shows only the number of whole months that have elapsed since last July 4. It does not show the total number of months since July 4 1776. Calculating recurring dates with the DatePeriod class Working out recurring dates such as the second Tuesday of each month is now remarkably easy thanks to the DatePeriod class. It
đang nạp các trang xem trước