NEWS

[PHP] Chia sẽ hàm định dạng ngày tháng giống facebook

[PHP] Chia sẽ hàm định dạng ngày tháng giống facebook
Đăng bởi: Thảo Meo - Lượt xem: 5763 08:10:06, 02/10/2017C#   In bài viết

Bài viết hôm nay, mình sẽ chia sẽ cho các bạn hàm định dạng ngày tháng giống facebook (Facebook style time ago).

[PHP] Function Format datetime like facebook style time ago

Các bạn có thể theo dõi hình ảnh định dạng ngày tháng của facebook như hình bên dưới:

dinh dang facebook ngay gio

Source code PHP:

<?php  
 date_default_timezone_set('America/New_York');  
 echo facebook_time_ago('2016-03-11 04:58:00');  
 function facebook_time_ago($timestamp)  
 {  
      $time_ago = strtotime($timestamp);  
      $current_time = time();  
      $time_difference = $current_time - $time_ago;  
      $seconds = $time_difference;  
      $minutes      = round($seconds / 60 );           // value 60 is seconds  
      $hours           = round($seconds / 3600);           //value 3600 is 60 minutes * 60 sec  
      $days          = round($seconds / 86400);          //86400 = 24 * 60 * 60;  
      $weeks          = round($seconds / 604800);          // 7*24*60*60;  
      $months          = round($seconds / 2629440);     //((365+365+365+365+366)/5/12)*24*60*60  
      $years          = round($seconds / 31553280);     //(365+365+365+365+366)/5 * 24 * 60 * 60  
      if($seconds <= 60)  
      {  
     return "Just Now";  
   }  
      else if($minutes <=60)  
      {  
     if($minutes==1)  
           {  
       return "one minute ago";  
     }  
     else 
           {  
       return "$minutes minutes ago";  
     }  
   }  
      else if($hours <=24)  
      {  
     if($hours==1)  
           {  
       return "an hour ago";  
     }  
           else 
           {  
       return "$hours hrs ago";  
     }  
   }  
      else if($days <= 7)  
      {  
     if($days==1)  
           {  
       return "yesterday";  
     }  
           else 
           {  
       return "$days days ago";  
     }  
   }  
      else if($weeks <= 4.3) //4.3 == 52/12  
      {  
     if($weeks==1)  
           {  
       return "a week ago";  
     }  
           else 
           {  
       return "$weeks weeks ago";  
     }  
   }  
       else if($months <=12)  
      {  
     if($months==1)  
           {  
       return "a month ago";  
     }  
           else 
           {  
       return "$months months ago";  
     }  
   }  
      else 
      {  
     if($years==1)  
           {  
       return "one year ago";  
     }  
           else 
           {  
       return "$years years ago";  
     }  
   }  
 }  
 ?>


HAVE FUN :)

THÔNG TIN TÁC GIẢ

BÀI VIẾT LIÊN QUAN

[PHP] Chia sẽ hàm định dạng ngày tháng giống facebook
Đăng bởi: Thảo Meo - Lượt xem: 5763 08:10:06, 02/10/2017C#   In bài viết

CÁC BÀI CÙNG CHỦ ĐỀ

Đọc tiếp
.