Page 1 of 2

[SOLVED] - Date Structuring

Posted: Tue Dec 19, 2006 5:19 pm
by iknownothing
Hey All,
With the site I am currently developing, a user has the ability to set a due date, as well as set a frequency for a task to be completed (weekly, fortnightly, monthly). Now, all was fine until I realised I had done a very stupid thing and converted all input data to integers instead of dates, so when a user selects monthly for example, being in December already, the next one goes to 13, or a month could have anywhere up to a million days... So, my question is how do I set it up so that it is recognised as a date, or at least acts like a date.

Current stupidity, goes something like this:

Code: Select all

if ($frequencydate = date("dmy")-1)
    if ($frequency == 'monthly') {
        $frequencydate = $frequencydate + 100; //100 being DD MM YY (DD M1 00)
}
I was thinking something like this, but is it still bad coding and/or do you think it will work...

Code: Select all

if ($frequencydate = date("dmy")-1)
    if ($frequency == 'monthly') {
        $frequencydate = $frequencydate + date("m");
}

Posted: Tue Dec 19, 2006 5:26 pm
by feyd
Stop using integers. strtotime() is where it's at.

Code: Select all

date('Ymd',strtotime('+100 days'));

Posted: Tue Dec 19, 2006 5:29 pm
by iknownothing
so this would work also (instead of the +100 days??

Code: Select all

date('Ymd',strtotime('+1 month'));

Posted: Tue Dec 19, 2006 5:40 pm
by Kieran Huggins
If you had put that string into a php file instead of the post form, you would have had your answer faster :wink:

Posted: Tue Dec 19, 2006 5:42 pm
by iknownothing
no worries, thanks for ya help!!

Posted: Tue Dec 19, 2006 5:45 pm
by RobertGonzalez
iknownothing wrote:so this would work also (instead of the +100 days??

Code: Select all

date('Ymd',strtotime('+1 month'));
Yes, but I would seriously recommend you do some trial and error locally when using strtotime(). It can be a bit weird in its interaction with system times and what time you think it is versus what time it actually is to the server.

Posted: Tue Dec 19, 2006 6:51 pm
by iknownothing
Everah wrote:
iknownothing wrote:so this would work also (instead of the +100 days??

Code: Select all

date('Ymd',strtotime('+1 month'));
Yes, but I would seriously recommend you do some trial and error locally when using strtotime(). It can be a bit weird in its interaction with system times and what time you think it is versus what time it actually is to the server.
its all good, the server is out by 1 hour, but be because the site will only be used from 9-5 (as its an internal site for work) it is unnoticeable, but thanks, I will take it into consideration in the future.

Posted: Tue Dec 19, 2006 7:52 pm
by iknownothing
Hey all again. I ammended the date thing to work, and when I echo it it works great, but doesnt seem to want to edit the data within the database, is it perhaps because its a query inside a query.

I'll give a general rundown of whats supposed to happen here. When a user has selected the type of frequency they would like and submit the task is goes into the database all fine, it also adds a due date for the frequency. If for example a weekly frequency was set, the user would set a due date, if that date is then surpassed, it will set itself to be due in a week from the original...

(comp means completed (1) or not completed (0))

The particular item also only appears on its due date.

the question: Can you see why it wouldnt work?

Code: Select all

else {
  $showtask = MYSQL_QUERY("SELECT * FROM tasks WHERE comp = '0'");
}
 
 
 while ($row = mysql_fetch_assoc($showtask))
  {

  	  $id = $row['id'];
          $frequency = $row['frequency'];
  	  $frequencycurrent = $row['frequencycurrent'];
  	  
   if ($frequencycurrent < date('dmy')){
    	if ($frequency == 'monthly') {
        $frequencycurrent = date('dmy',strtotime('+1 month'));
        mysql_query("UPDATE tasks SET frequencycurrent = '$frequencycurrent' comp = 1 WHERE id = '$id'", $con);
		}
		elseif ($frequency == 'fortnightly') {
			 $frequencycurrent = date('dmy',strtotime('+2 weeks'));
			 mysql_query("UPDATE tasks SET frequencycurrent = '$frequencycurrent' comp = 1 WHERE id = '$id'", $con);
		 }
		elseif ($frequency == 'weekly') {
	 	 $frequencycurrent = date('dmy',strtotime('+1 week'));
			 mysql_query("UPDATE tasks SET frequencycurrent = '$frequencycurrent', comp = 1 WHERE id = '$id'", $con);
		 }
		
		}
		if ($frequencycurrent == date('dmy')){
	  		mysql_query("UPDATE tasks SET comp = 0 WHERE id = '$id'", $con);
			}

}

Posted: Tue Dec 19, 2006 8:10 pm
by Kieran Huggins
I'm not sure if this is the correct behaviour or not, but you're not advancing the date in the database by one month with:

Code: Select all

$frequencycurrent = date('dmy',strtotime('+1 month'));
All that does is creates a date one month from now.

If you want to add a month to a date that isn't now, you must first seed the strtotime() with a date. Example:

Code: Select all

strtotime('January 5 2007 +1 month')
Also, have you considered storing your dates as either a unix timestamp or a MySQL datestring?

I prefer timestamps myself, often supplementing them with month, year and or weekday fields for easier querying. That way you can select a range of dates easily with the timestamp, and grouping by month, etc.. is a breeze.

Cheers,
Kieran

Posted: Tue Dec 19, 2006 8:17 pm
by iknownothing
I definitely will next time, but I've gone too deep with what I'm doing, and because its only a small internal website, it will get very limited usage, so as long as it works, it works!

thanks for your help too.

Posted: Tue Dec 19, 2006 8:19 pm
by John Cartwright
Kieran Huggins wrote:If you want to add a month to a date that isn't now, you must first seed the strtotime() with a date.
That is what the second argument for strtotime() is for :wink:

Posted: Tue Dec 19, 2006 8:27 pm
by Kieran Huggins
Jcart wrote:
Kieran Huggins wrote:If you want to add a month to a date that isn't now, you must first seed the strtotime() with a date.
That is what the second argument for strtotime() is for :wink:
whooooaaaaaa.......... 8O

I've never seen that there before... you learn something new every day! :) Thanks Jcart!

Cheers,
Kieran

Posted: Tue Dec 19, 2006 9:20 pm
by iknownothing
ok, it all works apart from this bit.

Its, as said by Keiran, taking the value for NOW. what i need is to somehow replace 'dmy' with the variable of '$frequencycurrent', so it adds 2 weeks onto the variable, not the current date.

Code: Select all

$frequencycurrent = date('dmy',strtotime('+2 weeks'));
these are things i've already tried, some, with some very unusual results...

Code: Select all

$frequencycurrent = date('$frequencycurrent',strtotime('+2 weeks'));

Code: Select all

$frequencycurrent = strtotime('+2 weeks');

Code: Select all

$frequencycurrent = $frequencycurrent + date(strtotime('+2 weeks'));

Code: Select all

$frequencycurrent = date(strtotime('+2 weeks'));

Code: Select all

$frequencycurrent = $frequencycurrent + strtotime('+2 weeks');
"$frequencycurrent" is in the exact format of "dmy"

Posted: Tue Dec 19, 2006 9:44 pm
by feyd
Here's a hint: it has to do with single quotes and not needing them.

Posted: Tue Dec 19, 2006 9:58 pm
by iknownothing
I'm guessing you meant:

Code: Select all

$frequencycurrent = date($frequencycurrent,strtotime('+2 weeks'));
Tried that, but its coming up with the original $frequencycurrent value.