What i would like to do is have a text that says this item was added XXX min/hrs/days/months/years ago.
i have a datetime field in my sql table that i would like it to be read from.
I'm a total noob at this and i know i'm already over my head. But i love to be over my head.
Let me know of you want any more information.
Thanks
Chris
Min Hrs Days Months Script
Moderator: General Moderators
- Christopher
- Site Administrator
- Posts: 13596
- Joined: Wed Aug 25, 2004 7:54 pm
- Location: New York, NY, US
Re: Min Hrs Days Months Script
have you looked through these functions: http://us3.php.net/manual/en/ref.datetime.php. There are many examples if you poke around. Maybe strtotime() to get a timestamp, then subtract from time().
(#10850)
Re: Min Hrs Days Months Script
i had a look at the site you gave me and i could not find exactly what i was looking for.
what i want is like what YouTube has on there website, and on http://news.google.com/nwshp?hl=en&tab=wn
http://www.youtube.com/user/foxbroadcasting
so what i believe i need to do is get local server time subtract time item was added then convert the results to what youtube and news.google.com have on there websites.
Chris
what i want is like what YouTube has on there website, and on http://news.google.com/nwshp?hl=en&tab=wn
http://www.youtube.com/user/foxbroadcasting
so what i believe i need to do is get local server time subtract time item was added then convert the results to what youtube and news.google.com have on there websites.
Chris
Re: Min Hrs Days Months Script
Are you wanting to use a generic 365 day year, and a generic 30/31 day month, or do you want it to be exact?
If generic is fine, you need division and modulus, and to convert your datetime column to a UNIX timestamp (at least for your calculations)
Figure out how many seconds are in a year - divide your date by that number & floor() it - that's your number of years. Modulus your date by the number of seconds in a year & that's the number you need to work on from here on out.
Figure out how many seconds are in a month - divide your date by that number & floor() it - that's your number of months. Modulus your new number by the number of seconds in a month & that's the number you need to work on from here on out.
... and so on
If generic is fine, you need division and modulus, and to convert your datetime column to a UNIX timestamp (at least for your calculations)
Figure out how many seconds are in a year - divide your date by that number & floor() it - that's your number of years. Modulus your date by the number of seconds in a year & that's the number you need to work on from here on out.
Figure out how many seconds are in a month - divide your date by that number & floor() it - that's your number of months. Modulus your new number by the number of seconds in a month & that's the number you need to work on from here on out.
... and so on
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Re: Min Hrs Days Months Script
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:
Posting Code in the Forums to learn how to do it too.
Code: Select all
<?php
$date1 = strtotime(date('d-M-Y h:i:s')); // current date and time
$date2 = strtotime("17-July-2008 16:30:30"); //date and time stored in database
$dateDiff = $date1 - $date2;
echo "Days : " . $fullDays = floor($dateDiff/(60*60*24));
echo "<br>";
echo "Hours : " . $fullHours = floor(($dateDiff-($fullDays*60*60*24))/(60*60));
echo "<br>";
echo "Minutes : " . $fullMinutes = floor(($dateDiff-($fullDays*60*60*24)-($fullHours*60*60))/60);
?>pickle | Please use [ code=php ], [ code=text ], etc tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read: