2

I want to create a reminder for my app, but if I compare the 2 dates, the outcome is always 'ok', even if the condition isn't true. Here's the code of the index.php:

<?php
require_once 'app/init.php';

date_default_timezone_set('Europe/Brussels');

$itemsQuery = $db->prepare("
    SELECT id, name, done, reminder
    FROM items
    WHERE user = :user
");

$itemsQuery->execute(array(
    ':user' => $_SESSION['user_id']
));

$items = $itemsQuery->rowCount() ? $itemsQuery : array();
?>

<!doctype html>
<html>
<head>
    <meta charset="UTF-8">
    <title>Scorpio To Do</title>

    <link href="http://fonts.googleapis.com/css?family=Open+Sans" rel="stylesheet">
    <link href="http://fonts.googleapis.com/css?family=Shadows+Into+Light+Two" rel="stylesheet">
    <link href="css/main.css" rel="stylesheet">

    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0">
</head>
<body>
    <div class="list">
        <h1 class="header">Scorpio To Do</h1>

        <?php if(!empty($items)): ?>

        <ul class="items">
            <?php foreach($items as $item): ?>
                <li>
                    <a href="indexAdvanced.php?as=advanced&item=<?php echo $item['id']; ?>"><span class="item<?php echo $item['done'] ? ' done' : '' ?>"><?php echo $item['name']; ?></span></a>
                    <?php if(!$item['done']): ?>
                        <a href="mark.php?as=done&item=<?php echo $item['id']; ?>" class="done-button">Done</a>
                    <?php elseif($item['done']): ?>
                        <a href="mark.php?as=notdone&item=<?php echo $item['id']; ?>" class="done-button">Not Done</a>
                        <a href="mark.php?as=del&item=<?php echo $item['id']; ?>" class="done-button">Delete</a>
                    <?php endif; ?>
                </li>

                <!--/*it's all about this code here*/-->

                <a href="#" name="something" id="something">
                    <?php 
                        echo $datum = $item['reminder']; //echoes good
                        echo " - "; 
                        $nu = new DateTime('NOW'); 
                        echo $nu->format('Y-m-d H:i:s'); //echoes good
                        echo " - "; 
                        if(strtotime($datum) >= $nu){ //comes out wrong
                            echo 'ok';
                        } else{
                            echo 'nok';
                        }
                    ?>
                </a>


            <?php endforeach; ?>
        </ul>
        <?php else: ?>
            <p>You havn't added any items yet!</p>
        <?php endif; ?>

        <form class="item-add" action="add.php" method="post">
            <input type="text" name="name" placeholder="Type something here" class="input" autocomplete="off" required>
            <input type="submit" value="Add" class="submit">
        </form>
    </div>

    <script src="js/jquery-1.9.1.js"></script>
    <script type="text/javascript" src="js/scorpiotodo.js"></script>
</body>
</html>
gvlasov
  • 18,638
  • 21
  • 74
  • 110
user3906610
  • 99
  • 1
  • 10
  • 2
    Ask yourself: what is `$nu` and what is `$item['reminder']`? Do they use the same format? What does `var_dump` tell you about them? How does comparison of differently represented variables work in PHP? Why does `DateTime` have a `getTimestamp` method (http://php.net/manual/en/datetime.gettimestamp.php)? – Sergiu Paraschiv Feb 09 '15 at 13:01

3 Answers3

2

the condition should be like this

# convert the datetime to unix

$str_nu = strtotime($today->format('Y-m-d H:i:s'));
# check two unix timestamp
if(strtotime($datum) >= $str_nu){ //comes out wrong
    echo 'ok';
} else{
    echo 'nok';
}

here is a link

Why can't I access DateTime->date in PHP's DateTime class? Is it a bug?

Community
  • 1
  • 1
Oli Soproni B.
  • 2,774
  • 3
  • 22
  • 47
  • 1
    Thanks, didn't notice. Thought it was correct since I did a 'format' function on it, but it also had to be 'strtotime' :) – user3906610 Feb 10 '15 at 07:08
0

You are comparing a formatted date with a timestamp and that doesn't work. Try to replace your $nu variable with time():

 if(strtotime($datum) >= time()){
Wim Mostmans
  • 3,485
  • 1
  • 20
  • 20
0

That's because you are comparing object (DateTime) to timestamp. Use getTimestamp() on that first.

if(strtotime($datum) >= $nu->getTimestamp()) {
Elon Than
  • 9,603
  • 4
  • 27
  • 37