Min Hrs Days Months Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
raggy99
Forum Newbie
Posts: 13
Joined: Mon Aug 14, 2006 6:52 am

Min Hrs Days Months Script

Post by raggy99 »

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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Min Hrs Days Months Script

Post by Christopher »

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)
raggy99
Forum Newbie
Posts: 13
Joined: Mon Aug 14, 2006 6:52 am

Re: Min Hrs Days Months Script

Post by raggy99 »

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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Min Hrs Days Months Script

Post by pickle »

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
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
mavieng
Forum Newbie
Posts: 10
Joined: Thu Jan 29, 2009 12:34 pm

Re: Min Hrs Days Months Script

Post by mavieng »

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: :arrow: 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: :arrow: Posting Code in the Forums to learn how to do it too.
Post Reply