Ezio Posted September 16, 2013 Share Posted September 16, 2013 Hey Guys, so here is the problem i am having.. I have saved a time in a table using mysql's now() function now i want to subtract the current time, in my php script with that of the table to check if one month has passed or not. How do i go about this..?? (The now() function in mysql returns date like : 2013-09-16 10:55:14) Please Help. Link to comment Share on other sites More sharing options...
Lulzim Posted September 16, 2013 Share Posted September 16, 2013 (edited) Something like this? $dateFromDb = new DateTime('2013-08-16 10:55:14');$currentDate = new DateTime();$difference = $currentDate->diff($dateFromDb);if($difference->m>=1){ echo "years: ".$difference->y."<br />"; echo "months: ".$difference->m."<br />"; echo "days: ".$difference->d."<br />"; echo "hours: ".$difference->h."<br />"; echo "minutes: ".$difference->i."<br />"; echo "seconds: ".$difference->s."<br />";} Edited September 16, 2013 by Lulzim Link to comment Share on other sites More sharing options...
Ezio Posted September 16, 2013 Author Share Posted September 16, 2013 (edited) Something like this: if($current_date_time - $date_time_from_db > 30){ echo"1 month has passed please register." }else{ echo "Carry on"; } please help. Edited September 16, 2013 by Ezio Link to comment Share on other sites More sharing options...
Lulzim Posted September 16, 2013 Share Posted September 16, 2013 You just had to make a few small changes: <?php$dateFromDb = new DateTime($replace_this_with_your_date_from_db); #don't forget to fix this line$currentDate = new DateTime();$difference = $currentDate->diff($dateFromDb);if($difference->m>=1){ echo "1 month has passed please register.";}else { echo "Carry on"; } Link to comment Share on other sites More sharing options...
Ezio Posted September 16, 2013 Author Share Posted September 16, 2013 Ok, Let me try this. Thanks a lot. Link to comment Share on other sites More sharing options...
Ezio Posted September 17, 2013 Author Share Posted September 17, 2013 It's not working Link to comment Share on other sites More sharing options...
rootKID Posted September 17, 2013 Share Posted September 17, 2013 (edited) not sure if i'm getting this right, but in your SQL, you would be checking first of all if it's older than 30 days, so something like this: // 30 days; 24 hours; 60 mins; 60secs (shows WHERE less than 30 days)$res = mysql_query("SELECT * FROM TABLE WHERE YOUR_TIME + ( 30 *24 *60 *60 ) > ".time()." ORDER BY YOUR_TIME DESC LIMIT 2"); Note that this is just a method i use myself, but something similar to this one, i use this peace of code for showing all the latest news items for my news website that are less than 30 days, and if bigger, dont... the ORDER BY and after there you can change after wish, but something similar to this code i think after what i read and understand of your wishes... Hope it helps a little .. EDIT Lulzim also have something close to my method, sort of x)... Edited September 23, 2013 by rootKID Link to comment Share on other sites More sharing options...
rootKID Posted September 18, 2013 Share Posted September 18, 2013 DUMP: Did it help? Or what?... Link to comment Share on other sites More sharing options...
Ezio Posted September 23, 2013 Author Share Posted September 23, 2013 Thanks. It was helpful. :-) Link to comment Share on other sites More sharing options...
rootKID Posted September 23, 2013 Share Posted September 23, 2013 ok, note that this was just a quick one, might wonna change stuff here and there and try to play around with it, but i think it's something close by to this one. That is what i imagine when i read your post, not sure if its the same.. but still Link to comment Share on other sites More sharing options...
Ezio Posted September 23, 2013 Author Share Posted September 23, 2013 Actually i wanted to fetch the date & time from my mysql database (I had used now() to store the date and time) and subtract it later with the current date and time to check if x number of months has passed or not. Link to comment Share on other sites More sharing options...
rootKID Posted September 23, 2013 Share Posted September 23, 2013 hmm, sounds interesting.. could you give an example? So i might understand it better.. Link to comment Share on other sites More sharing options...
Ezio Posted September 25, 2013 Author Share Posted September 25, 2013 I have saved the users subscription date and time using now() in the database and when a user logs in to his profile i subtract the current date and time from the date and time from database to check if a month has passed or not.. ..it works fine if i take a static days of 30 but what if a month has 31 days..?? Link to comment Share on other sites More sharing options...
justsomeguy Posted September 25, 2013 Share Posted September 25, 2013 If you want one month then you need the month, date, and year of the subscription date and current date. You can figure out from that if one month has passed. If the current month is not January, then the years and dates need to be the same and the subscription month needs to be one less. If the current month is January, then the year needs to be one less and the previous month needs to be December. Link to comment Share on other sites More sharing options...
Ezio Posted September 26, 2013 Author Share Posted September 26, 2013 ah...i am confused.. Link to comment Share on other sites More sharing options...
rootKID Posted September 26, 2013 Share Posted September 26, 2013 @JSG is it not possible to do with a normal (time()) function? Just that i have a function from an old project of mine that can predect the month by time function, all i'm saying is if it's not possible to do same here? Example would be the SQL i just shared above. I'm currently using that SQL for my News table where added is a simple date_time() (or normal time()) function (if i remember correctly), and using time to simply check the month, not that hard once getting the hang of it... So when i see your post, i'm getting a little bit confuced. But again, not sure if my method is 100% sure and effective, but i dont complain.. worked so far. Link to comment Share on other sites More sharing options...
justsomeguy Posted September 26, 2013 Share Posted September 26, 2013 You can get the month from a timestamp, or something like a DateTime object. So you can have 2 timestamps, or 2 DateTime objects, get the month, date, and year from each, and compare them. You can also use DateTime::diff to get the difference between 2 dates as a DateInterval object and then get the number of days, months, and years in the interval. http://www.php.net/manual/en/datetime.diff.php Link to comment Share on other sites More sharing options...
Ezio Posted September 27, 2013 Author Share Posted September 27, 2013 Got it...thanks :-) Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now