-1

I want $dayleft inside the else to appear in "Y/n/j" format, but when I run it, it only returns the year, what is happening?

Code:

$select = mysqli_query($connect, $query_select);
$row = mysqli_fetch_row($select);
$idchange = $row['5'];
$date = DateTime::createFromFormat("Y-n-j", $idchange);
if (date('Y/n/j', strtotime("-60 days")) > $date){
echo "<br><a href='changeid.php'>Reset Machine_ID</a> - Status: <font color='green'>Available</font>";
}
else{
$dayleft = date('Y/n/j', strtotime("+60 days")) - $date;
echo "<br><a href='#'>Reset Machine_ID</a> - Status: <font color='red'>Unavailable</font> - Available Day: $dayleft";
}
Francisco
  • 431
  • 9
  • 16
  • You can't compare strings with `>`. Make them integers, or use the `datediff` function. – chris85 May 09 '17 at 00:46
  • `date()` produces a string. You're using strings in mathematical comparisons and operations. The results will be somewhat random. – deceze May 09 '17 at 00:55
  • @chris85 I did that: `if (date_diff(date('Y/n/j'),$date) >= 60)` and is producing this error: `Warning: date_diff() expects parameter 1 to be DateTimeInterface, string given in /home/u478317930/public_html/externalhack.php on line 92 ` – Francisco May 09 '17 at 01:05
  • @deceze how i do in the right way? – Francisco May 09 '17 at 01:06
  • What exactly do you *want* to do? Print the difference between two dates as "X years, Y months, Z days"? Or simply subtract/add days to a date and produce a new date? – deceze May 09 '17 at 01:09
  • @deceze print the difference. – Francisco May 09 '17 at 01:11
  • Use `strtotime` and PHP match functions, or use `datediff`. http://php.net/manual/en/datetime.diff.php – chris85 May 09 '17 at 02:51

1 Answers1

0

You have to format the final result of your calculation.

$dayleft = date('Y/n/j', strtotime("+60 days")) - $date;
$dayleft = date('Y/n/j',$dayleft);
echo "<br><a href='#'>Reset Machine_ID</a> - Status: <font color='red'>Unavailable</font> - Available Day: $dayleft";

UPDATE:

Here's an example tested in PHP Fiddle:

<?php
$date = time();
echo "Today: ". date('Y/m/d h:n:s',$date)."<br />";

//$dayleft = date_add(date('Y/m/d h:i:s',strtotime("+60 days")), $date);
$t1 = date('Y/m/d h:i:s',strtotime('+60 day', $date));
echo "Date Available: " . $t1."<br />";
$dayleft = date('Y/n/j',strtotime($t1));
echo $dayleft."<br />";
echo "<br><a href='#'>Reset Machine_ID</a> - Status: <font color='red'>Unavailable</font> - Available Day: $dayleft";
?>

And here's the output:

Today: 2017/05/08 09:5:48

Date Available: 2017/07/07 09:57:48 2017/7/7

Reset Machine_ID - Status: Unavailable - Available Day: 2017/7/7

Sloan Thrasher
  • 4,953
  • 3
  • 22
  • 40