Several methods for obtaining php time-php Tutorial

Source: Internet
Author: User
Several methods for obtaining php time

  1. Function getWeekName ($ data, $ format = 'week ')
  2. {
  3. $ Week = date ("D", $ data );
  4. Switch ($ week)
  5. {
  6. Case "Mon ":
  7. $ Current = $ format. "1 ";
  8. Break;
  9. Case "Tue ":
  10. $ Current = $ format. "2 ";
  11. Break;
  12. Case "Wed ":
  13. $ Current = $ format. "3 ";
  14. Break;
  15. Case "Thu ":
  16. $ Current = $ format. "4 ";
  17. Break;
  18. Case "Fri ":
  19. $ Current = $ format. "5 ";
  20. Break;
  21. Case "Sat ":
  22. $ Current = $ format. "6"; break;
  23. Case "Sun ":
  24. $ Current = $ format. "day ";
  25. Break;
  26. }
  27. Return $ current;
  28. }
  29. Echo 'Today is: '. getWeekName (time (), 'week ');
  30. Echo'
    ';
  31. Echo 'Today is: '. getWeekName (time (), 'week ');
  32. Echo'
    ';
  33. Echo '2017-12-12 is: '. getWeekName (strtotime ('2017-12-12'), 'week ');
  34. ?>

4. get the user-defined functions for the effects such as a few hours ago posted in a similar article

  1. Function time2Units ($ time)
  2. {
  3. $ Year = floor ($ time/60/60/24/365 );
  4. $ Time-= $ year * 60x60*24*365;
  5. $ Month = floor ($ time/60/60/24/30 );
  6. $ Time-= $ month * 60*60*24*30;
  7. $ Week = floor ($ time/60/60/24/7 );
  8. $ Time-= $ week * 60*60*24*7;
  9. $ Day = floor ($ time/60/60/24 );
  10. $ Time-= $ day * 60*60*24;
  11. $ Hour = floor ($ time/60/60 );
  12. $ Time-= $ hour * 60*60;
  13. $ Minute = floor ($ time/60 );
  14. $ Time-= $ minute * 60;
  15. $ Second = $ time;
  16. $ Elapse = '';
  17. $ UnitArr = array ('year' => 'year', 'month' => 'month', 'week' => 'week ', 'Day' => 'day ',
  18. 'Hour '=> 'hour', 'mine' => 'Minute ', 'second' => 'second'
  19. );
  20. Foreach ($ unitArr as $ cn => $ u)
  21. {
  22. If ($ u> 0)
  23. {
  24. $ Elapse =$ u. $ cn;
  25. Break;
  26. }
  27. }
  28. Return $ elapse;
  29. }
  30. $ Past = 2052345678; // release date
  31. $ Now = time (); // Current date
  32. $ Diff = $ now-$ past; // difference value
  33. Echo 'posted in'. time2Units ($ diff ';
  34. ?>

In other words, I think it is better:

  1. Function time_tran ($ the_time ){
  2. $ Now_time = date ("Y-m-d H: I: s", time () + 8*60*60 );
  3. $ Now_time = strtotime ($ now_time );
  4. $ Show_time = strtotime ($ the_time );
  5. $ Dur = $ now_time-$ show_time;
  6. If ($ dur <0 ){
  7. Return $ the_time;
  8. } Else {
  9. If ($ dur <60 ){
  10. Return $ dur. 'seconds ago ';
  11. } Else {
  12. If ($ dur <3600 ){
  13. Return floor ($ dur/60). 'minute ago ';
  14. } Else {
  15. If ($ dur <86400 ){
  16. Return floor ($ dur/3600). 'hour ago ';
  17. } Else {
  18. If ($ dur <259200) {// within 3 days
  19. Return floor ($ dur/86400). 'Days ago ';
  20. } Else {
  21. Return $ the_time;
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }
  28. ?>

5. calculate the time difference based on the difference

  1. Function getTime ($ val ){
  2. If ($ val> 0 ){
  3. $ NTime ['nday'] = (int) ($ val/(3600*24 ));
  4. $ NTime ['nhour'] = (int) ($ val % (3600*24)/3600 );
  5. $ NTime ['nmin'] = (int) ($ val % (3600*24) % 3600/60 );
  6. $ NTime ['nss'] = (int) ($ val % (3600*24) % 3600% 60 );
  7. }
  8. Return $ nTime;
  9. }
  10. Function getStrTime ($ val ){
  11. $ ATime = getTime ($ val );
  12. $ Dtoc = array ('nday' => 'day', 'nhour' => 'hour ', 'nmin' => 'min ', 'nss' => 'second ');
  13. If ($ aTime ){
  14. Foreach ($ aTime as $ k => $ v ){
  15. If ($ v ){
  16. $ CTime. = $ v. $ dtoc [$ k];
  17. }
  18. }
  19. } Else {
  20. $ CTime = 'stopped ';
  21. }
  22. Return $ cTime;
  23. }
  24. ?>

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.