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?

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?
I made this, it basically works abit like facebooks timer (under status' & posts etc..)
Edit it to how you need it
Enjoy, Lew.PHP Code: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);
}
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![]()
try this:
PHP Code:<?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());
?>
I'm not crazy, ask my toaster.
That's not what he asked for though, he wants to display the number of days, not seconds/minutes/hours.I made this, it basically works abit like facebooks timer (under status' & posts etc..)
Edit it to how you need it
Enjoy, Lew.PHP Code: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);
}
That wouldn't work, it says time() - time().try this:
PHP Code:<?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());
?>
I'd do this:
Untested and rushed, but should work.PHP Code: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);
Code:<?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. ?>
Thanks! Works perfectlyThat'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:
Untested and rushed, but should work.PHP Code: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);
I'm sure you did code that. http://snipplr.com/view/4912/relative-time/I made this, it basically works abit like facebooks timer (under status' & posts etc..)
Edit it to how you need it
Enjoy, Lew.PHP Code: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);
}
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.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 editI'm sure you did code that. http://snipplr.com/view/4912/relative-time/
Lew.
Want to hide these adverts? Register an account for free!