Click here to Skip to main content
15,896,207 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am creating a small auction page and when a user posts an item, the time and date are stored in the database using deafult value CURRENT_TIMESTAMP.

PHP
$posted = $result['datePosted'];

$date=date_create($posted);
date_add($date,date_interval_create_from_date_string("7 days"));
$futureDate = date_format($date,"d-m-Y H:i");

					date_default_timezone_set('GB-Eire');
					$now = date('d-m-Y H:i');
					echo $now.'<br>'. $futureDate;
					if ($now >= $futureDate){
						$futureDate = 'Expired';
					} else {
						$futureDate = date_format($date,"d-m-Y H:i");
					}

I echo'd $now and $futureDate so I could see when futureDate (the date that is 7 days past the posted date) would be less than the current date (which I presumed would mean it would in the past).

However it doesnt seem to work. I dont know how to do it but I think it may have something to do with the format of the date or the data type (can a string be less than another string?), maybe I need to convert it to integer values?

Im not sure.
Posted
Comments
Sergey Alexandrovich Kryukov 14-Apr-15 15:31pm    
"Doesn't seem to work" is not informative. Can you use the debugger?
—SA

1 solution

This is a problem: $now = date('d-m-Y H:i');

You're first part of the date is the day - then won't the 23-03-53 come after 15-03-77 ? If you must, for some reason, compare dates as strings then you need the dates to be formatted 'yyyy-mm-dd' etc.

Also, you must use the 2-digit version of month and day so that 10, 11, and 12 are greater than 2,3,4, etc.

Better still - you're using a database: if we pretend it's SQL Server, the current moment is gotten by getDate(): why not check with your SQL query, instead? Then you know the date and time formats will be in agreement.
 
Share this answer
 
Comments
jba1991 15-Apr-15 8:25am    
Thanks. I want to check it with the database date. Would I use getDate() for the current date and then compare that to the date in the database? Also, is timestamp a string?

Also, to make 10,11 and 12 greater than 2,3,4 etc would I just change ('d-m-y') to ('dd-mm-yyyy')?
W Balboos, GHB 15-Apr-15 8:38am    
If you check it in SQL and it's a datetime field (it should be!) then you simply compare getdate > yourFieldName (or <, etc) and let SQL worry about it.

In SQL, php, javaScript, and almost anything else, datetime is an object. It contains multiple data values that can be accessed as required. It is not a string.

If you still wish, for some odd reason, to do your comparison via strings then I suggest you find (and bookmark) the php date object formatting information. "Y-m-d", however, will do what you need. If you're not going to actually print the dates then leave out the hyphens.
jba1991 15-Apr-15 8:49am    
Yeah, my database date is DATETIME and is set using default value CURRENT_TIMESTAMP. So can i get the database date (as on object, not as string) and then compare it with getDate() and it should work?
W Balboos, GHB 15-Apr-15 8:57am    
I don't know what database you're using - but the answer to your question is yes.

Generally, you want to let the database engine do what it's best at (selecting records with a query language) and then return only what you actually need. You can request all records, for example, entered before some specific datatime and use those in your auction, or max bid up to a certain date (although you should stop taking data, instead, no?)

Anyway - you need to decide how to run your auction.

When you've decided you're questions answered (on this or any submitted question), please mark an answer as accepted so that the question is closed.
jba1991 15-Apr-15 9:28am    
Okay I understand what you are saying and it definately gave me an idea in how to solve my problem. I didnt even think of only displaying data after a certain date!

I will ensure (as always) that I will mark the answer as accepted once I have fixed it, but I will wait in case I need to expand my question.

Thanks again

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900