4

Can someone out there help me with this:

<?php 
    include 'include/db_config.php';

    $result = $dbs->prepare("SELECT * FROM service_info WHERE id = :id");
    $result->bindParam(':id', $_SESSION['id']);
    $result->execute();
    for ($i=0;$i = 0; $row = $result->fetch(); $i++) {
        ?>

        <td><?php echo $row['datepicker']; ?></td>

        <?php



        $offset = strtotime("+1 day");
        $enddate = date("Y-m-d H:i:s", $offset);
        echo $enddate . "</br>";
        if ($enddate < $startdate) {
            echo "expired";
        } else {
            echo "active";
        }
    }
?>

What I want to achieve is to find out if the date value held in $row['datepicker'] was more than 2 days ago. If it was more than 2 days ago I want it to echo expired and otherwise I want it to show active.

For example: $row['datepicker'] could contain the date: May 18, 2016 (based on the users input - not a fixed value). That would mean it's expiration date would be: May 20,2016. If today's date is greater than or equal to May 20th 2016, it should echo expired. If today's date was May 19th 2016 it should echo active because it is not yet two days in the past.

Henders
  • 1,195
  • 1
  • 21
  • 27
  • 4
    This code doesn't make any sense nor is your question clear. – John Conde May 18 '16 at 13:57
  • Related : http://stackoverflow.com/questions/5883571/get-next-and-previous-day-with-php – apokryfos May 18 '16 at 14:01
  • What do you want?? Explain Clearly! – Jenson M John May 18 '16 at 14:01
  • where is $stardate coming from? you can compare timestamps (before passing through the date) so you can use if($offset < strtotime($startdate)) { ... – Richard May 18 '16 at 14:02
  • i want to make an expiration date. but my code doesnt seem to work . i am making a massage website where in it has a reservation and it automatically expires every 2 days @JensonMJohn –  May 18 '16 at 14:40
  • Your start date just needs to be the value of the `$row['datepicker']` right? And then you need to compare them as you are but at timestamps. – Henders May 18 '16 at 14:49
  • And what is the value of `$row['datepicker']` currently? What do you get if you do `echo $row['datepicker'];`? – Henders May 18 '16 at 14:53
  • @Henders if i do echo $row['datepicker'] the value of it is 11-May-2016 and i chooses that date and yeah its working . i just dont know how to compare it with the 2 days interval so that it will expire every 2 days –  May 18 '16 at 14:56
  • So if the current date today is 13-May-2016 you want it to say expired? – Henders May 18 '16 at 15:00
  • @Henders precisely . can you help me sir ? . –  May 18 '16 at 15:01

2 Answers2

7

Based on your comments, you want to take the $startdate and add two days to it. If today's date is passed that time then you want to print expired and otherwise print active.

$startdate = "16-May-2016";
$expire = strtotime($startdate. ' + 2 days');
$today = strtotime("today midnight");

if($today >= $expire){
    echo "expired";
} else {
    echo "active";
}

In this case it will echo expired because, at the time of writing this, we are two days passed the 16th May. If you changed the $startdate to be 17th May it would echo active because that is only 1 day in the past.

For your specific problem you would want to change $startdate to look like this:

$startdate = $row['datepicker'];

Whenever the date specified in $startdate is greater than or equal to 2 days in the past it will show expired otherwise it will show active.


What are we doing here?

It's quite simple. We take advantage of strtotime() to get the timestamp of the day it will be +2 days from your $startdate value. Then we get today's date at midnight (so the actual timestamp would be the equivalent of 18-May-2016 00:00:00).

Our last step is to compare the two timestamps and see if today's date is greater than or equal to the expiry date.


Note: This assumes that your $startdate is in a format that is compatible with strtotime().

Community
  • 1
  • 1
Henders
  • 1,195
  • 1
  • 21
  • 27
  • so i should write my code like this? $startdate = $row['datepicker']; $expire = strtotime($startdate. ' + 2 days'); $today = strtotime("today midnight"); if($today >= $expire){ echo "expired"; } else { echo "active"; } –  May 18 '16 at 15:20
  • Yeah, I think that should work. – Henders May 18 '16 at 15:21
  • it doesnt work sir . i got this error: Uncaught exception 'Exception' with message 'DateTime::__construct() [datetime.--construct]: Failed to parse time string (11 Maypm16 2017) at position 6 (p): The timezone could not be found in the database' –  May 18 '16 at 15:32
  • @Ginxx0009 There is an issue with the date you are trying to pass in. As your example you said the value of `$row['datepicker']` would be `11-May-2016` which works for the code I have provided. You are attempting to pass it a string like this: `11 Maypm16 2017` which is different to what you said you were passing. It looks like the value of your `$row['datepicker']` is the issue. – Henders May 18 '16 at 15:35
  • I used varchar not date sorry i forgot to say . Does it affect sir? @Henders –  May 18 '16 at 15:38
  • @Ginxx0009 that isn't the issue. The issue is in your code where you are attempting to create a DateTime object with the string of `11 Maypm16 2017`. If you want my solution to work you will need to remove your attempt in the final PHP tags which looks to be the cause of the issue (as it's the only place where you attempt to create a new dateTime – Henders May 18 '16 at 15:45
  • And now it works i just changed varchar into date but the thing is in returns 0000-00-00 expired –  May 18 '16 at 15:48
  • You are passing the value of 0000-00-00 as your `$startdate`. If you pass in the date as you said you were doing before with `11-May-2016` it will work. You're close to getting it to work you just need to make sure the value of `$row['datepicker']` is in the correct format (dd-Month-YYYY) – Henders May 18 '16 at 15:51
  • It is actually working because it is more than 2 days since the year 0000 which is why it says `expired`. You just need your code to pass the date you want. – Henders May 18 '16 at 15:55
  • on my database the format of my date is 0000-00-00 so should i follow my database date format . cause here in my datepicker its script> $(document).ready(function() { $("#datepicker").datepicker({ changeMonth:true, changeYear:true, dateFormat:"dd-mm-yy" }); }); –  May 18 '16 at 15:57
  • i was such an idiot . ofcourse i should put a data first on my database to work . i forgot i change it from varchar to date. >.< what a mess. im really sorry for making it hard for you sir >. –  May 18 '16 at 16:01
  • So is it working now? – Henders May 18 '16 at 16:01
  • im just a bit confused right here sir . $today_date) echo 'expired'; else{ echo 'active'; its how can i exactly determine that after 2 days it'll expire –  May 18 '16 at 16:04
  • Look at my code example. I don't have `$offset = strtotime("+1 day");` in it. That is something you've added. In my example, you can determine how many days it expires in by editing the value of: `$expire = strtotime($startdate. ' + 2 days');` and changing `+ 2 days` to be `+ 12 days` for example. – Henders May 18 '16 at 16:12
1

In php 5.2+ you can use this

$today = date("Y-m-d H:i:s");  
$startdate = $row['datepicker'];   
$offset = strtotime("+1 day");
$enddate = date($startdate, $offset);    
$today_date = new DateTime($today);
$expiry_date = new DateTime($enddate);

if ($expiry_date < $today_date) { // your echo stuff }
Amit Ray
  • 3,445
  • 2
  • 19
  • 35
  • 1
    What is `$startdate`? It doesn't seem to have been defined. – Henders May 18 '16 at 14:36
  • what i mean by my code was the should be the starting date and may end date would be after 2 days and automatically expires . but i cant do it >. –  May 18 '16 at 14:43
  • @Ginxx0009 Your $startdate is that only where you are picking the date from datepicker. be careful how you are storing the date in database. Otherwise this code will work without fail. – Amit Ray May 18 '16 at 14:48
  • @AmitRay so how can i pass the value of my datepicker . can i do it like this ? : $DateToday = Datepicker? –  May 18 '16 at 14:51
  • you can do this $start_date = $_POST["datepicker"]; as your date format will be in form 18 May 2016 . To save into database use strtotime $timestamp = strtotime($start_date); – Amit Ray May 18 '16 at 15:04
  • @AmitRay sir can you do it in a form of code . I'm really sorry but im just new to php –  May 18 '16 at 15:06
  • For a datepicker in form you can use something like this http://code.runnable.com/UdTuotHbZoQNAABq/adding-a-date-picker-to-an-input-form-field-using-jquery . To get the form data use $start_date = $_POST["datepicker"]; To save into database use $datetime = strtotime($start_date); To retrieve the date use $time = date("Y-m-d H:i:s",$row->datetime); Then compare the two dates. – Amit Ray May 18 '16 at 15:14
  • @AmitRay it says; Uncaught exception 'Exception' with message 'DateTime::__construct() [datetime.--construct]: Failed to parse time string (11 Maypm16 2017) at position 6 (p): The timezone could not be found in the database' in –  May 18 '16 at 15:16
  • Can you paste the whole code? It is difficult to know what exactly you are doing. I request you to edit the question and paste the entire code – Amit Ray May 18 '16 at 15:21
  • @AmitRay i paste my whole code sir –  May 18 '16 at 15:33