[SOLVED] - Date Structuring

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

User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

[SOLVED] - Date Structuring

Post 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");
}
Last edited by iknownothing on Wed Dec 20, 2006 7:54 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Stop using integers. strtotime() is where it's at.

Code: Select all

date('Ymd',strtotime('+100 days'));
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

so this would work also (instead of the +100 days??

Code: Select all

date('Ymd',strtotime('+1 month'));
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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:
Last edited by Kieran Huggins on Tue Dec 19, 2006 5:42 pm, edited 1 time in total.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

no worries, thanks for ya help!!
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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);
			}

}
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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:
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post 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
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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"
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Here's a hint: it has to do with single quotes and not needing them.
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post 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.
Post Reply