Time and Date

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

tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Time and Date

Post by tommy1987 »

I have the following fields stored in a mySQL database:

Date, in the format: 2006-10-15
Time, in the format: 01:30:54

I want to be able to take this information and display how long ago it was since today. e.g. 4 seconds ago, or 2 minutes ago, or 1 day ago, or 1 month ago.

What is the best way of doing this, will I need to write my own algorithm to calculate it?


Thanks Tom
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

look for printf's function in Code Snippets.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

excuse my ignorance but where is the code snippets?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post by Oren »

Double posting haa feyd... One of the mods/admins should ban you for that :lol:
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Oren wrote:Double posting haa feyd... One of the mods/admins should ban you for that :lol:
huh, weird.. when I thought I may have double posted that and checked, it wasn't there. Oh well, deleted.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

I cant seem to find the piece of code specified in the code snippets forum, any other ideas?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

search.php
Search for Author: printf
Forum: Code Snippets

returns two results
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

A very valuable tool on our forums is the search feature.

Make sure you set the criteria to author "printf" and forum is set to "Code Snipplets"
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

i beg your pardon, I thought you were referring to a function called printf.

Sorry :oops:
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

no excuse ;)
even a search for the keyword:printf only returns four results for the forum: Code Snippets
* (Swift) PHP Mailer class with plugin support
* Automatic php2js convertion
* time -> seconds to array(y,w,d,h,m,s)
* Array Debugging
:twisted:
sujon
Forum Newbie
Posts: 2
Joined: Thu Oct 12, 2006 12:50 am

Re: Time and Date

Post by sujon »

tommy1987 wrote:I have the following fields stored in a mySQL database:

Date, in the format: 2006-10-15
Time, in the format: 01:30:54

I want to be able to take this information and display how long ago it was since today. e.g. 4 seconds ago, or 2 minutes ago, or 1 day ago, or 1 month ago.

What is the best way of doing this, will I need to write my own algorithm to calculate it?


Thanks Tom
Sujon wrote: Actually u can write ur own algorithm by using this concept
$currentDate = date("Y-m-d");
$yesterday= date("Y-m-d", mktime(0, 0, 0, date("m") , date("d")-1, date("Y")));
$tomorrow= date("Y-m-d", mktime(0, 0, 0, date("m") , date("d")+1, date("Y")));
I hope u'll solve ur pproblem.
Thanks.
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

this is exactly what I want but...

I need to get the time elapsed since the original time to whatever the date and time it is currently in seconds to then pass to this function.
Does anybody know of a way to do this, since obviously different months have different amounts of days etc..
To clarify the problem, imagine a forum and someone posts, then when redrawing the page in future the script will calculate how long ago this was in seconds then pass this to printf's function.


Hope there is something which someone knows of which makes my job a bit easier.

Thanks in advance for any help.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Code: Select all

time() - mktime(...);
tommy1987
Forum Commoner
Posts: 92
Joined: Tue Feb 21, 2006 8:35 pm

Post by tommy1987 »

thanks very much for that. Looks to be just what I am after..
Post Reply