Learning how to convert timestamp and date formats in php

Source: Internet
Author: User
Learning how to convert timestamp and date formats in php

  1. /*-------------------------------------------------------
  2. *
  3. * Copyright (c) 2011 Baidu.com, Inc. All Rights Reserved
  4. * $ Id $
  5. *
  6. *-------------------------------------------------------*/
  7. // Time stamp to date
  8. $ Date_time_array = getdate (1297845628); // 1311177600 1316865566
  9. $ Hours = $ date_time_array ["hours"];
  10. $ Minutes = $ date_time_array ["minutes"];
  11. $ Seconds = $ date_time_array ["seconds"];
  12. $ Month = $ date_time_array ["mon"];
  13. $ Day = $ date_time_array ["mday"];
  14. $ Year = $ date_time_array ["year"];
  15. Echo "year: $ year \ nmonth: $ month \ nday: $ day \ nhour: $ hours \ nminutes: $ minutes \ nseconds: $ seconds \ n ";
  16. // Normal date to timestamp
  17. Echo mktime (0, 0, 0, 9, 18,201 1). "\ n ";
  18. Echo mktime (0, 0, 0, 9, 25,201 1). "\ n ";
  19. /*
  20. Time ();
  21. Returns the current time, but returns an integer.
  22. */
  23. // You can format it.
  24. Echo "time () display Year Month Day hour minute second:". date ("Y-m-d H: I: s", time (). "\ n ";
  25. // Display in minutes
  26. Echo "time () shows only the year, month, and day:". date ("Y-m-d", time (). "\ n"; // only the year, month, and day are displayed.
  27. Echo "timestamp format:". date ("Y-m-d H: I: s", 1297845628). "\ n"; // use the timestamp directly.

  28. // Convert a normal date to a timestamp. The same is true if there is a time, minute, and second,

  29. $ Year = (int) substr ("",); // Obtain the year
  30. $ Month = (int) substr ("",); // Obtain the month
  31. $ Day = (int) substr ("",); // Obtain the number of digits
  32. Echo mktime (0, 0, 0, $ month, $ day, $ year );

  33. /* Vim: set ts = 4 sw = 4 sts = 4 tw = 100 noet :*/

  34. ?>

Note: 1) There are two types of PHP Time difference: one is the timestamp type (1228348800) and the other is the normal date format)

2) the timestamp above php5.1 is 8 hours different from the actual time. the solution is as follows: 1. the simplest method is not to use php5.1 or a later version-obviously this is not an advisable method !!! 2. modify php. ini. Open php. search for date in ini. remove the semicolon = Asia/Shanghai from timezone and restart the apache server. The disadvantage is that if the program is placed on another server, php cannot be modified. ini. 3. add the initialization statement of time to the program: "date_default_timezone_set (" Asia/Shanghai ");" which can be set by the programmer at will. I recommend this statement. Time zone identifier, available value in mainland China: PRC, Asia/Chongqing, Asia/Shanghai, Asia/Urumqi (China, Chongqing, Shanghai, Urumqi), Etc/GMT-8, asia/Harbin Hong Kong and Taiwan regions are available: Asia/Macao, Asia/Hong_Kong, Asia/Taipei (Macau, Hong Kong, and Taipei in sequence) and Singapore: asia/Singapore can be output in Beijing time.

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.