0

When I am processing two dates within a php script, the output that is rendered is not what is being expected.

For example when I run

<?php
date_default_timezone_set('EST');
$firstday = date('Y-m-01');
$today = date('Y-m-d');

if ($firstday = $today)
    echo "Today is the 1st <br/>";
else 
    echo "Today is not the 1st <br/>";

echo'The first of the month is: '. $firstday . '<br/>';
echo'Today is: '. $today . '<br/>';
?>

The output is:

Today is the 1st

The first of the month is: 2014-07-30

Today is: 2014-07-30

When it should be

Today is not the 1st.

The first of the month is: 2014-07-01

Today is: 2014-07-30

However when I run one of the date()s at a time, I get that $today = 2014-07-30 and I get $firstdate = 2014-07-01 but not when I run them at the same time.

Can you not run two date() functions at the same time in PHP or do I have it formatted wrong?

3
  • 2
    if ($firstday = $today) <-- you use = instead of ==. Commented Jul 30, 2014 at 14:03
  • 1
    Was just going to say that ^^^ you're assigning instead of comparing. Plus, use braces man. Commented Jul 30, 2014 at 14:04
  • Wow. Duh. I had that before and I must had accidently deleted it. Commented Jul 30, 2014 at 14:07

1 Answer 1

2

Don't assign $today to $firstdate or vice-versa Do $firstday == $today

date_default_timezone_set('EST');
$firstday = date('Y-m-01');
$today = date('Y-m-d');

if ($firstday == $today)
    echo "Today is the 1st";
else 
    echo "Today is not the 1st";
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.