Mike Squire
Mike Squire

Reputation: 43

Show text after a date has passed

I am trying to get a simple line of text to appear if todays date is after another date.

I can either get it to appear on all pages or none, but I am unable to get it to display based on whether the challenge start date is before or after todays date. I believe it could be a date format issue, but everything I have tried has fallen short.

Here is my code:

Get todays date

$date_now = new dateTime();

Challenge start date

$challengeStartDate = date('dS F Y', strtotime($this->item->start_date));
echo '<!--' . strtotime('1970/1/1 00:00:00 +' . $validity) . '-->';

New text line

if ($challengeStartDate > $date_now) echo "New Text";

Upvotes: 4

Views: 193

Answers (2)

Peter Pointer
Peter Pointer

Reputation: 4170

date() returns a string. With $challengeStartDate > $date_now it's like comparing if one string is bigger than the other (not sure if your dateTime handles that).

Your approach is otherwise fine. Just use timestamps to compare. time() gets you the time as a Unix timestamp:

$now = time();
if ($now > strtotime($this->item->start_date)) {
  // do your thing
}

Something like this is more what you need. Try it out.

Upvotes: 1

user14979715
user14979715

Reputation:

I had the very same problem some time ago. All you need to do is store your local time in a database so it would be saved statically. Because in your example, both $challengeStartDate and $date_now will change and update simultaneously and you wiill always get the current pc time! Try storing it in a table or idk maybe sessions would help too.

Upvotes: 0

Related Questions