This topic is locked

compare date field to 'now()'

1/2/2007 11:18:35 AM
PHPRunner General questions
T
thesofa author

This one has me stumped, I am trying to compare the date time held in a date field to now().

if I echo both the values to the screen, I get the date field output as

'2007-01-07 00:00:00' but the value of now() shows as

2007-01-02 16-07-45 without any quotes.

The if clause reads thus

else if ($dfdv>=now())
$dfdv is a date field value

even when $dfdv is greater than now(), it does not enter the loop, please tell me what I am doing wrong

L
larsonsc 1/2/2007

This one has me stumped, I am trying to compare the date time held in a date field to now().

if I echo both the values to the screen, I get the date field output as

'2007-01-07 00:00:00' but the value of now() shows as

2007-01-02 16-07-45 without any quotes.

The if clause reads thus

else if ($dfdv>=now())
$dfdv is a date field value

even when $dfdv is greater than now(), it does not enter the loop, please tell me what I am doing wrong


I had a similar problem to this related to date based comparisons. Do you need to be able to compare the timestamp as well or just the date portion? I only needed to compare the date portion of the field, so I was able to achieve this in an event. Let me know what you portions of the datetime you need to compare, and I'll get you an else if parameter to pass for the comparison.

T
thesofa author 1/2/2007

Just the date part please
Did you try the dynamic report?

L
larsonsc 1/2/2007

OK, here is how I achieved the date comparison:

else if ((date("z", strtotime($dfdv))) >= date("z")) {

// perform some action

}


What this does is convert the date value to a numeric value of what day of the year has been parsed. For example 2007-01-01 has a "z" value of 001. This takes the timestamp out of the equation. Let me know if this works for you.

T
thesofa author 1/2/2007

Ha, that was quick!

I have found a different way around this, that seems to work.

I have used substr, like this

else if (substr($dfdv,1,10)>=now())



this takes the characters from 1 for 10, count starts at 0 BTW.

so it misses the ' at the start of the data.

Thanks for the help anyhow.

Happy new Year too.

L
larsonsc 1/2/2007

Ha, that was quick!

I have found a different way around this, that seems to work.

I have used substr, like this
this takes the characters from 1 for 10, count starts at 0 BTW.

so it misses the ' at the start of the data.

Thanks for the help anyhow.

Happy new Year too.


Didn't think about using substring. Nice work. I'll have to keep that in mind in the future.

D
Dale 1/2/2007

Happy New Year,
Another way to kill this issue is
else if ($dfdv>=(date('Y-m-d'))
do action.
Comparing the field to now() is getting hit by the changing seconds and milleseconds in the now() result.

T
thesofa author 1/2/2007

that wasn't the problem, the problem is the apostrophe around the value from the date field, whereas now() does not have them.