Click here to Skip to main content
15,888,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I am trying to basically, store current date, get the date of when user registered and compare to set number of days which is 7
See code below - basically, the user is allowed 7 days to complete something. What I want to do is, Set the amount of days to 7 and then this can be compared with the dates so it outputs how many days they have left..so when they first go onto the web page, itl start of with 7 and countsdown the days 6,5,4,3,2,1... each day

Can someone help with the below?


What I have tried:

<?php

include ("dbConnect.php");
$UserID =$_SESSION['currentUserID'];

$dbQuery=$conn->prepare("select Date from Results where UserID=$UserID");
$dbQuery->execute();

  while($dbRow = $dbQuery->fetch(PDO::FETCH_ASSOC)) {
  $StartDate=$dbRow['Date'];
   $CurrDate= date("Y-m-d");   
    $Days=7;
-added 
function dayCount($CurrDate, $StartDate) {
    $first_date = strtotime($StartDate);
    $second_date = strtotime($CurrDate);
    $days_diff = $second_date - $first_date;
    return date('d',$days_diff);
}

 }
Posted
Updated 27-Mar-18 1:36am
v2

Use a PHP: time - Manual[^] value. It is easier to calculate differences.
 
Share this answer
 
Comments
Maciej Los 27-Mar-18 7:17am    
5ed!
Member 13637584 27-Mar-18 7:18am    
it just seems to be be bringing me back 0 no matter what?
Richard MacCutchan 27-Mar-18 7:28am    
You are using strings as your date values. You cannot calculate differences using strings, you must use numeric types.
Member 13637584 27-Mar-18 7:29am    
Can you help me please?
Richard MacCutchan 27-Mar-18 7:50am    
Times are Unix time values so the difference will be the number of seconds. You need to divide by 86400 to get the number of days:

$first_date = strtotime("03/04/18");
var_dump($first_date);
$second_date = strtotime("03/27/18");
var_dump($second_date);
$secs_diff = $second_date - $first_date;
var_dump($secs_diff);
$days = $secs_diff / 86400;
var_dump($days);
Since your doing this from a database,

1) store dates as actual datetimes
2) use the datediff function (SQL) or whatever's close in your db.

The return value can be something like 7-DATEDIFF(...), which you should cast as an int so it's a whole number.
 
Share this answer
 
Comments
Member 13637584 27-Mar-18 7:37am    
Can you help me with using my code? thank you
W Balboos, GHB 27-Mar-18 7:41am    
Both myself and answer (1) have supplied you with some help. Problems pointed out and alternate methods of getting date difference.

That's help. "Do my work for me", if that's what you mean, is not help.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900