Jump to content

Need Help Here..


Ezio

Recommended Posts

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

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 by Lulzim
Link to comment
Share on other sites

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

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 :P..

 

EDIT

Lulzim also have something close to my method, sort of x)...

Edited by rootKID
Link to comment
Share on other sites

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

hmm, sounds interesting.. could you give an example? So i might understand it better..

Link to comment
Share on other sites

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

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

@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

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

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...