There is trend in showing time like 1 min ago, 5 hour ago instead of full date time this type timing mostly using on showing the time of social media post the time of post comparing to current date time with time zone and calculate the difference between current date time to posted media date time if difference is in second the it show some second ago(ex:difference is 1 second the its show 1 second ago) and if difference is in minutes the it show some minute ago (ex:difference is 10 minute the its show 10 minute ago) and show on up to year.
there is simple logic for converting date time to ago just convert all the time (year, month,day, hour ,minute) to second and compare current datetime and get the differences and put ago in last of that differences.
<?php function time_to_ago($custom_time) { $custom_time = strtotime($custom_time); $date_time_now_string = date('Y-m-d H:i:s'); $date_time_now = strtotime($date_time_now_string); $time = $date_time_now - $custom_time; if ($time < 1){ return '0 seconds'; } $timer_count = array( 365 * 24 * 60 * 60 => 'year', 30 * 24 * 60 * 60 => 'month', 24 * 60 * 60 => 'day', 60 * 60 => 'hour', 60 => 'minute', 1 => 'second' ); $timer_code = array( 'year' => 'years', 'month' => 'months', 'day' => 'days', 'hour' => 'hours', 'minute' => 'minutes', 'second' => 'seconds' ); $finel_time=''; foreach ($timer_count as $key => $value){ $dates = $time / $key; if ($dates >= 1){ $round_of = floor($dates); $time_remove = $round_of * $key; if(($time - $round_of) < 0){ $time -= ($round_of - 1) * $key; } else{ $time -= $time_remove; } $finel_time = $finel_time . $round_of . ' ' . ($round_of > 1 ? $timer_code[$value] : $value) . ' '; } } return $finel_time . ' ago'; } date_default_timezone_set("Asia/kolkata"); //echo date_default_timezone_get(); //echo date('Y-m-d H:i:s'); echo time_to_ago('2018-12-09 23:38:00'); ?>
Hello, My Name is pradip parker . I am a Web Developer, Professional Blogger from India. i love and like web designing and developing, i have two year of experience in programming and developing I am the founder of programmingconcept. I started this blog in 2017 to for help developers by providing easy,small and best tutorials.