PDA

View Full Version : PHP to display number of days since given date?



Flumples
23-01-2011, 01:26 PM
I have very little PHP knowledge, usually I can find anything out with a google search but can't find this anywhere.

I need to display the number of days since a given date, basically I need a counter that increases by 1 each day.

Any ideas?

LMS16
23-01-2011, 01:46 PM
I made this, it basically works abit like facebooks timer (under status' & posts etc..)

Edit it to how you need it :)



function howLong($timestamp) {
$diff = time()-$timestamp;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
return "on " . date("jS F, Y", $timestamp);
}


Enjoy, Lew.

Flumples
23-01-2011, 05:12 PM
Thanks! You're gonna have to help me to get it to display on a page though, I really am SUCH a php noob!! I've tried wrapping <?php and ?> tags round it but didn't work, probably doing it all wrong :P

HotelUser
23-01-2011, 05:23 PM
Thanks! You're gonna have to help me to get it to display on a page though, I really am SUCH a php noob!! I've tried wrapping <?php and ?> tags round it but didn't work, probably doing it all wrong :P


try this:



<?php
function howLong($timestamp) {
$diff = time()-$timestamp;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
return "on " . date("jS F, Y", $timestamp);
}

howLong(time());
?>

Trinity
23-01-2011, 05:34 PM
I made this, it basically works abit like facebooks timer (under status' & posts etc..)

Edit it to how you need it :)



function howLong($timestamp) {
$diff = time()-$timestamp;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
return "on " . date("jS F, Y", $timestamp);
}


Enjoy, Lew.

That's not what he asked for though, he wants to display the number of days, not seconds/minutes/hours.


try this:



<?php
function howLong($timestamp) {
$diff = time()-$timestamp;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
return "on " . date("jS F, Y", $timestamp);
}

howLong(time());
?>


That wouldn't work, it says time() - time().

I'd do this:


function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}

// Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
$stampystamp = 1295803279;

// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo getRelativeTime($stampystamp);



Untested and rushed, but should work.

coolchris322
23-01-2011, 05:52 PM
<?php
function howLong($timestamp) {
$diff = time()-$timestamp;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
return "on " . date("jS F, Y", $timestamp);
}

howLong("second" + "minutes" + "hours" + "days" + "months" + "years"); //Set a value in each of them.
?>

Flumples
23-01-2011, 06:06 PM
That's not what he asked for though, he wants to display the number of days, not seconds/minutes/hours.



That wouldn't work, it says time() - time().

I'd do this:


function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}

// Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
$stampystamp = 1295803279;

// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo getRelativeTime($stampystamp);



Untested and rushed, but should work.

Thanks! Works perfectly

BoyBetterKnow
23-01-2011, 07:14 PM
I made this, it basically works abit like facebooks timer (under status' & posts etc..)

Edit it to how you need it :)



function howLong($timestamp) {
$diff = time()-$timestamp;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
return "on " . date("jS F, Y", $timestamp);
}


Enjoy, Lew.

I'm sure you did code that. http://snipplr.com/view/4912/relative-time/

Trinity
23-01-2011, 07:18 PM
I'm sure you did code that. http://snipplr.com/view/4912/relative-time/

That's the one I used, I added the link in the first time I wrote the post, but then I had to go back to include HotelUser's post and forgot to add it in.

LMS16
23-01-2011, 07:43 PM
I'm sure you did code that. http://snipplr.com/view/4912/relative-time/

Sorry if you thought I said I made that exact thing, I meant edit :P

Lew.

Dentafrice
24-01-2011, 07:14 AM
No... basically you said you MADE it... "I made this, it basically works abit like facebooks timer (under status' & posts etc..)".

LMS16
24-01-2011, 12:11 PM
No... basically you said you MADE it... "I made this, it basically works abit like facebooks timer (under status' & posts etc..)".

Hence why I said I meant edit :)

Lew.

HotelUser
24-01-2011, 12:56 PM
Hence why I said I meant edit :)

Lew.

I think it might be best to call a spade a spade and just move on - next time perhaps use a better vocabulary when posting snippets :)

Dentafrice
24-01-2011, 06:48 PM
Now HotelUser, a spade isn't a spade! :) There's box shovels, round shovels, plumbers shovels, gardening shovels.

Well.. a spade's a spade I reckonnn!

Flumples
19-02-2011, 03:44 PM
That's not what he asked for though, he wants to display the number of days, not seconds/minutes/hours.



That wouldn't work, it says time() - time().

I'd do this:


function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}

// Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
$stampystamp = 1295803279;

// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo getRelativeTime($stampystamp);



Untested and rushed, but should work.

Sorry, don't mean to bump, but this has broken now! :S

It now displays 'on January 1, 1970' as the output.. any ideas?

Trinity
19-02-2011, 04:00 PM
Sorry, don't mean to bump, but this has broken now! :S

It now displays 'on January 1, 1970' as the output.. any ideas?

Significance of that date in case it helps - http://en.wikipedia.org/wiki/January_1,_1970
Post the code that you're actually using.

Flumples
19-02-2011, 04:03 PM
Thanks, I haven't actually changed anything yet cos it's the same as was posted earlier. It was working originally..

Trinity
19-02-2011, 04:41 PM
Thanks, I haven't actually changed anything yet cos it's the same as was posted earlier. It was working originally..

Yes, so post the code that you're using to call that function.
Also, this is probably not the problem, but check your server's time/date settings.

Flumples
19-02-2011, 05:25 PM
function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}

// Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
$stampystamp = 1295803279;

// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo getRelativeTime($stampystamp);

Trinity
19-02-2011, 11:19 PM
function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}

// Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
$stampystamp = 1295803279;

// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo getRelativeTime($stampystamp);



No. You've just posted the code that I posted. Post the code that you are using to call the function.
It should look a bit like this:


echo getRelativeTime(1295803279);


You've probably used a variable instead of that timestamp though, so post the code that you use to assign a value to the variable too.

Flumples
20-02-2011, 01:32 AM
I'm confused... That's exactly how I've got it coded on my page.

E.g.



<html>
<head>
</head>
<body>
<?php


function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}

// Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
$stampystamp = 1295803279;

// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo getRelativeTime($stampystamp);


?>
</body>
</html>

I take it I'm doing something wrong..? Sorry like I said earlier my php knowledge is very limited!

Trinity
20-02-2011, 02:07 AM
I'm confused... That's exactly how I've got it coded on my page.

E.g.



<html>
<head>
</head>
<body>
<?php


function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";
return "on " . date("F j, Y", strtotime($date));
}

// Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
$stampystamp = 1295803279;

// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo getRelativeTime($stampystamp);


?>
</body>
</html>

I take it I'm doing something wrong..? Sorry like I said earlier my php knowledge is very limited!

Hmm I'm stumped then. I tested the code when I first posted it, it worked fine, but I just copied and pasted the exact same code and it's displaying the epoch thing. I wrongly assumed you were just making a stupid mistake at first, but I'm having the same problem.

Anyone else fancy jumping in on this? :)

Mr-Trainor
20-02-2011, 09:28 AM
I tried it yesterday as well and had the same problem. My PHP knowledge isn't much though :P. When I changed the timestamp (I change it too 2/3 days ago) it worked fine.

Johno
20-02-2011, 01:12 PM
<?php

date_default_timezone_set('Europe/London');

function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";

return "on " . date("F j, Y", $date);
}

// Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
$stampystamp = 1295803279;

// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo getRelativeTime($stampystamp);


?>

strtotime is used to "Parse about any English textual datetime description into a Unix timestamp" - The value that was being passed in was the timestamp :)

Flumples
20-02-2011, 03:24 PM
<?php

date_default_timezone_set('Europe/London');

function plural($num) {
if ($num != 1)
return "s";
}

function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff<7)
return $diff . " day" . plural($diff) . " ago";
$diff = round($diff/7);
if ($diff<4)
return $diff . " week" . plural($diff) . " ago";

return "on " . date("F j, Y", $date);
}

// Unix timestamp for today at 17:21:19, replace this with a timestamp for whenever you want
$stampystamp = 1295803279;

// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo getRelativeTime($stampystamp);


?>

strtotime is used to "Parse about any English textual datetime description into a Unix timestamp" - The value that was being passed in was the timestamp :)

Ooo thats sorted it, thanks! It's still not what I was after though.

I needed a counter that displayed the number of days since a certain date, not weeks or anything like that, just days. So a counter that increases by 1 each day. Basically it's to go on a news articles page thats styled to look like a newspaper and has 'Issue #6' or whatever in the top corner and I needed that issue number to go up by 1 each day just to make it look a bit more realistic.

I managed to sort it though, no idea how and this is probably really messy coding, but it seems to work :P


<?php
function plural($num) {
if ($num != 1)
return "s";
}
function getRelativeTime($date) {
$diff = time() - $date;
if ($diff<60)
return $diff . " second" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<60)
return $diff . " minute" . plural($diff) . " ago";
$diff = round($diff/60);
if ($diff<24)
return $diff . " hour" . plural($diff) . " ago";
$diff = round($diff/24);
if ($diff/7)
return $diff . "" . "";
return "on " . date("F j, Y", strtotime($date));
}
// Unix timestamp
$stampystamp = 1295803279;
// Display the number of seconds/minutes/hours/days/weeks since the timestamp
echo
?>

Source
21-02-2011, 03:12 AM
Just days you sayyy?



<?php

/**
* Function that you can c/p
**/

function daysSince( $date )
{
$from = strtotime( $date );
$to = time();
$time = $to - $from;

return round( ( ( ( $time / 60 ) / 60 ) / 24 ), 0);
}

/**
* Usage - should output 8 from 21st :)
**/

echo daysSince( '8 February 2011' );

?>


Hope that helps.

P.S You can tell when I'm stressed and up late when I post on HxF ^_^ and HAIII TRINITY

Trinity
21-02-2011, 03:27 AM
Just days you sayyy?



<?php

/**
* Function that you can c/p
**/

function daysSince( $date )
{
$from = strtotime( $date );
$to = time();
$time = $to - $from;

return round( ( ( ( $time / 60 ) / 60 ) / 24 ), 0);
}

/**
* Usage - should output 8 from 21st :)
**/

echo daysSince( '8 February 2011' );

?>


Hope that helps.

P.S You can tell when I'm stressed and up late when I post on HxF ^_^ and HAIII TRINITY

HAIII MATT :D

I don't want to get shouted at for making a 'pointless' post, so erm... that's some good code that. Flumples, you should totally use it.

Source
21-02-2011, 03:37 AM
We must catchup soon my dear.

If anyone is interested how the days are worked out, then yeah. A timestamp is the seconds that have passed since said 1970 date. Take the timestamp of today, minus the timestamp of the date you want to work out how many days have passed will leave you with how many seconds have passed from input date, to this second. From there it's as simple as dividing by 60 for minutes, dividing by 60 again for hours and lastly dividing by 24 to leave us with days.

Fun.

Dentafrice
21-02-2011, 04:34 PM
Mattt! I love you, k thx.

Anyways I don't care about a pointless post, so this is pointless.

Source
21-02-2011, 11:02 PM
Mattt! I love you, k thx.

Always knew it ^_^ hahaha, how are you Mr Mingle? Mingle Graphics going well?

Also for the sake of using less code, you could use something like:



<?php

/**
* Function that you can c/p
**/

function daysSince( $date )
{
return round( ( ( ( time() - strtotime( $date ) / 60 ) / 60 ) / 24 ), 0 );
}

/**
* Usage - should output 8 from 21st :)
**/

echo daysSince( '8 February 2011' );

?>

Want to hide these adverts? Register an account for free!