cmattoon Posted April 17, 2010 Share Posted April 17, 2010 I started a mysql database, with the following field being pertinent: dtg_start -- Stores the value of $dtg_start=date("m/d/Y_H:i:s", time());... This returns "04/17/2010_13:01:00" In my inexperience, I didn't store the UNIX epoch value, for functions like this... SO, without re-doing my database to include a "dtg_unix_start" field, then doing "then"-"now" (987654321-123456789) and converting the resulting UNIX time into "human time", is there a way to format now() into the same format, then use the "strtotime" feature to "subtract" strings from each other? Essentially, when a person is entered into the database, it timestamps their entry in the dtg_start field. I have a "view status" page that is a table that shows timestamp, then the person's information. I want to add an "Elapsed" field next to the timestamp to show how long the person has been in the system. I've read a little on the strtotime function, and it seems to be problematic, espicially when it comes to the last day of the month. Any ideas? (Or just suck it up and re-do the database? lol) Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/198866-another-stupid-new-guy-question-timestamp-elapsed-time/ Share on other sites More sharing options...
PFMaBiSmAd Posted April 17, 2010 Share Posted April 17, 2010 Mysql has a couple of dozen built in data/time functions that you can use to do just about anything with a DATE or DATETIME value. No slow parsed, tokenized, interpreted php code is needed for most things. You can convert your exiting data into a DATETIME by using the mysql STR_TO_DATE() function in a query. You can also retrieve a DATETIME in any format using the msyql DATE_FORMAT() function in your queries. Quote Link to comment https://forums.phpfreaks.com/topic/198866-another-stupid-new-guy-question-timestamp-elapsed-time/#findComment-1043854 Share on other sites More sharing options...
ddubs Posted April 17, 2010 Share Posted April 17, 2010 Using unix time-stamps gives you more flexibility in the long run. It shouldn't be too big of a change to modify the DB field to INT and start storing straight up time(); values in there. Quote Link to comment https://forums.phpfreaks.com/topic/198866-another-stupid-new-guy-question-timestamp-elapsed-time/#findComment-1043856 Share on other sites More sharing options...
cmattoon Posted April 17, 2010 Author Share Posted April 17, 2010 Ok, changed the database... added a u_start (UNIX_start) field... Put the following code into my PHP file to display the elapsed time.. $x_now = time(); $elapsed = $x_now-$row[21]; $elapsed = date("h:i:s", $elapsed); (example) echo "$elapsed"; The minutes/seconds are right, but it shows 6:04:32.. why 6 hours? is this a result of my timezone (Eastern US) and DST? Do i need to specify somewhere what timezone, even though I'm working off both UNIX epoch timestamps?? UGH Quote Link to comment https://forums.phpfreaks.com/topic/198866-another-stupid-new-guy-question-timestamp-elapsed-time/#findComment-1043868 Share on other sites More sharing options...
ddubs Posted April 17, 2010 Share Posted April 17, 2010 The UNIX time stamp is relative to local server time. Verify your clock source on the server as well as its configured time zone. You can also specify the timezone in the date() function. It should calculate Daylight Savings based on the date. Quote Link to comment https://forums.phpfreaks.com/topic/198866-another-stupid-new-guy-question-timestamp-elapsed-time/#findComment-1043871 Share on other sites More sharing options...
cmattoon Posted April 17, 2010 Author Share Posted April 17, 2010 That's what I thought... any idea why the 6 hour difference then? It's running off of the same server.. same directory/database/table even... Quote Link to comment https://forums.phpfreaks.com/topic/198866-another-stupid-new-guy-question-timestamp-elapsed-time/#findComment-1043873 Share on other sites More sharing options...
ddubs Posted April 17, 2010 Share Posted April 17, 2010 Make a test.php page containing just: <?php phpinfo(); ?> Look under date and see whats configured for local timezone. Quote Link to comment https://forums.phpfreaks.com/topic/198866-another-stupid-new-guy-question-timestamp-elapsed-time/#findComment-1043877 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.