Jump to content

Array for comment timestamps


DomMarx

Recommended Posts

Hey guys,

 

I was just wondering if anyone on here has some guidance for someone trying to build a comment time stamp (30 seconds ago, 1 minute ago, 1 month ago, 1 year ago, etc.) that takes the different day count of every month + leap year into consideration. I'm not asking for you guys to give me straight code or anything. I just need some guidance on finding the proper/best method of going about this. 

 

I'm guessing placing everything in arrays is a must?

 

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/279094-array-for-comment-timestamps/
Share on other sites

Use DateTime and DateInterval objects.

$timestamp = '2013-06-13 01:00:00';
$dt = new DateTime($timestamp);
$diff = $dt->diff(new DateTime())->format('%y,%m,%d,%h,%i,%s');

list ($y, $m, $d, $h, $n, $s) = explode(',', $diff);

edit: result $diff = 0,0,0,0,39,5

I also thought i'd ask a quick side question if you don't mind, since you seem pretty knowledgable(being a guru and all haha). When it comes to scalability, does this type of code need to be written differently? Let's say 50,000 users were posting on the website and this type of code was a function to display the comment timestamp, would it eventually break down if it isn't written in a special way? Or does scalability only apply to much larger functions?

 

Just wondering  :happy-04:

I tried the following to experiment, but the Seconds just start over at 0 when reaching 60. Could anyone tell me what is wrong here?

 

 


<?php


$actual_time = date('s');
    echo $actual_time." ".'Seconds ago ';
if ($actual_time >= 60){
$actual_time = strtotime("+ 1 minute");
echo $actual_time." ".'Minutes ago ';
}




?>

 

Also, thanks again for taking the time Barand. This forum is great!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.