Date and time in PHP

Source: Internet
Author: User
PHP has many easy-to-use functions to display and process dates. To display the date or time in a specific pattern, you can apply the date () function. It has two parameters: how to display the date pattern and the timestamp that represents the date you want to display. This timestamp PHP has many functions that are easy to use to display and process dates.

To display the date or time in a specific pattern, you can apply the date () function. It has two parameters: how to display the date pattern and the timestamp that represents the date you want to display. This timestamp must be the total number of seconds from January 1, 1970 as mentioned earlier. (if you want to apply the current time, you can apply the time () function, this function returns the "current" timestamp ). Date () has many pattern options, just like the strftime () function in C or the POSIX: strftime () function in Perl.


$ Birthday_stamp = mktime );

$ Birthday_formatted = date ('F d, Y-g: ia ', $ birthday_stamp );

Echo 'David was born on $ birthday_formatted .'

?>

Will display

David was born on March 10,197 5--7: 45 p. m.


Of course, if you need a specific known date, this complex pattern function will not be very useful. Because you already know what your situation will be. These functions are useful when processing the form output part that requires the user to select a date:

$ D = time ();For ($ I = 0; $ I <10; $ I ){Echo''. Date ('F d', $ d );USD d = 86400;}?>

The above will output a single region, with ten options-today and the next nine days. Before the program loop starts, we store the current time in the variable $ d. Every The value is displayed, and the value is calculated based on the Unix Timestamp. the displayed text is set to month and day ("July 27", "July 28", and so on ). After the value is displayed, the variable $ d is added with 86,400 (the total number of seconds of the 24 hours a day-24 hours * 60 Minutes * 60 seconds ).

By combining the mktime () and date () functions, you can obtain related information about the input date of a specific user. What if we want to find the first Sunday (or any day of the week) from a specific date? First, write a function that will output an appropriate pattern:


Functiondisplay_form (){

Global $ PHP_SELF;

$ Dotw = array ('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday ',

'Friday', 'Saturday ');

$ Months = array (1 => 'January ', 'February', 'March', 'marril', 'may', 'June ',

'July', 'August ', 'September', 'October ', 'November', 'December ');

?>



Find the first

For ($ I = 0; $ I <7; $ I ){Echo'$ Dotw [$ I] ';}Echo'After ';For ($ I = 1; $ I <= 12; $ I ){Echo'$ Months [$ I] ';}Echo' ';For ($ I = 1; $ I <= 31; $ I ){Echo'$ I ';}Echo', ';

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.