Several ways to get time from PHP

Source: Internet
Author: User
  1. function Getweekname ($data, $format = ' Week ')
  2. {
  3. $week = Date ("D", $data);
  4. Switch ($week)
  5. {
  6. Case "Mon":
  7. $current = $format. " One ";
  8. Break
  9. Case "Tue":
  10. $current = $format. " Two ";
  11. Break
  12. Case "Wed":
  13. $current = $format. " Three ";
  14. Break
  15. Case "Thu":
  16. $current = $format. " Four ";
  17. Break
  18. Case "Fri":
  19. $current = $format. " Five ";
  20. Break
  21. Case "Sat":
  22. $current = $format. " Six "; 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 (), ' worship ');
  32. Echo '
    ';
  33. Echo ' 2010-12-12 is: '. Getweekname (Strtotime (' 2010-12-12 '), ' worship ');
  34. ?>
Copy Code

4. Get a custom function that looks like a few hours before the article was published

  1. function Time2units ($time)
  2. {
  3. $year = Floor ($time/60/60/24/365);
  4. $time-= $year * 60 * 60 * 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 ' = ' + ' Years ', ' month ' = ' ' month ', ' Week ' = ' ' Week ', ' Day ' = ' ~ ',
  18. ' Hours ' = ' hour ', ' minutes ' = ' minute ', ' sec ' = ' 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 ' published in '. Time2units ($diff). ' former ';
  34. ?>
Copy Code

Another, personally considered 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). ' Minutes ago ';
    14. }else{
    15. if ($dur < 86400) {
    16. Return floor ($dur/3600). ' Hours 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. ?>
Copy Code

5, according to the two time difference between the specific calculation 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 [' nSec '] = (int) ($val% (3600*24)%3600%60);
    7. }
    8. return $nTime;
    9. }
    10. function Getstrtime ($val) {
    11. $aTime = GetTime ($val);
    12. $dtoc = Array (' nday ' = ' + ' days ', ' nhour ' = ' h ', ' nMin ' = ' min ', ' nSec ' = ' s ');
    13. if ($aTime) {
    14. foreach ($aTime as $k = = $v) {
    15. if ($v) {
    16. $cTime. = $v. $dtoc [$k];
    17. }
    18. }
    19. }else{
    20. $cTime = ' closed ';
    21. }
    22. return $cTime;
    23. }
    24. ?>
Copy Code
  • Related Article

    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.