calculate the difference between two dates with strtotime [duplicate] - php

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 8 years ago.
I want to echo how much days are left between a variable and NOW. This is the code
$now = date('Y-m-d');
$daysleft= strtotime($starttheproject -$now);
echo
"Now =".$now.
"<br> Starttheproject =".$starttheproject.
"<br> Daysleft =".$daysleft;
Result of echo:
Row =2014-10-17
Starttheproject =2014-10-22
Daysleft =
Question:
How can calculate the number of days, so that the result will be '5'
I've been playing with code like this, with no luck:
$daysleft= date('Y-m-d', strtotime($starttheproject -$now));

Try with this:
$date = new DateTime('2014-03-10');
$finish = new DateTime();
$difference = $date->diff($finish);
$difference =$difference->format('%R%a');
if ($difference > 10) { //if the post is over 10 days old
echo 'Renew now';
}else{
echo 'Renew in '.$difference.' days</font>';
}

Related

How to get 1 hours before [duplicate]

This question already has answers here:
Create Variable in PHP Equal to Current Time Minus One Hour
(8 answers)
Closed 3 years ago.
how to get 1 hours before in php
I try with:
but not working
$jam = "10:00:00";
$1hrsbefore = strtotime($jam), strtotime(-1 hours);
Try this code :
$jam = "10:00:00";
$timeadd= strtotime($jam) - 60*60;
$time = date('H:i:s', $timeadd);
echo $time;
Try this
$jam="10:00:00";
$time = date('H:i:s', strtotime($jam) - 60*60);
echo $time;

PHP - How to compare two different datetimes? [duplicate]

This question already has answers here:
Calculate time different in minute and second
(2 answers)
Closed 6 years ago.
In the database it is set for current_timestamp I am creating a script to run every hour that will compare the time from the Database then from local server see if it has been more than one hour so far I have this but confused where to go from here:
<?php
require('../config/dbconfig.php');
$sql = "SELECT * FROM stocks";
$result = mysqli_query($dbconfig,$sql);
while($row=mysqli_fetch_array($result,MYSQLI_ASSOC)) {
$time = $row["TimeBought"];
}
$Time = date("Y-m-d H:i:s",$time);
$DateTime = time();
$NewTime = strtotime($Time);
What should I do from here?
It is better to use DateTime object instead of date function.
$now = new DateTime();
$date = new DateTime('2014-06-04');
if ($now > $date) {
echo 'The date is in the past.';
} else {
echo 'The date is in the future.';
}

How to get all dates between two dates [duplicate]

This question already has answers here:
PHP: Return all dates between two dates in an array [duplicate]
(26 answers)
Closed 7 years ago.
I have check-in column and check-out column in my mysql table. I am posting this dates with UI-Datepicker.
eg:
I am trying to block the dates in the datepicker. I have dates like this in database
check in = 2015-06-15
check out = 2015-06-20
Now i want to get dates like this
2015-06-15,
2015-06-16,
2015-06-17,
2015-06-18,
2015-06-19,
2015-06-20
How to get dates like above I mentioned. So please help me and resolve my problem.
<?php
$date_from = strtotime("10 September 2000");
$date_to = strtotime("15 September 2000");
function list_days($date_from,$date_to){
$arr_days = array();
$day_passed = ($date_to - $date_from); //seconds
$day_passed = ($day_passed/86400); //days
$counter = 1;
$day_to_display = $date_from;
while($counter < $day_passed){
$day_to_display += 86400;
//echo date("F j, Y \n", $day_to_display);
$arr_days[] = date('o-m-d',$day_to_display);
$counter++;
}
return $arr_days;
}
print_r(list_days($date_from,$date_to));
?>

Check if date is later than current date [duplicate]

This question already has answers here:
How can I check if the current date/time is past a set date/time?
(4 answers)
Closed 9 years ago.
How can I check if the date is later than the current date?
if(date("d") < "18" && date("m") < "01") {
echo 'To late!';
}
Doesn't work for me.
You can do this notation:
if( '20140505' > date("Ymd") ) {
// today's date is before 20140505 (May 5, 2014)
}
$time1 = strtotime(date("d/m/Y", "18/01/2014"));
if(time() > $time1)
{
echo "too late!";
}
The best is to use strtotime.
$current_date = date("Y-m-d");
$date_to_compare = date("Y-m-d",time()+86400); //1 day later
if (strtotime($date_to_compare) > strtotime($current_date)) {
echo "too late";
}

Php calculete the cost for each day passed [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
PHP calculate days Between 2 different dates [duplicate]
(2 answers)
Closed 9 years ago.
$1date =$row['Date1'];
$2date = $row['Date2'];
$datediff = $1date - $2date;
echo $datediff;
I want to count the days between and put into a table the result(10 dollars for each day passed)
// convert to unix timestamp
$1date = strtotime($row['Date1']);
$2date = strtotime($row['Date2']);
// 86400 seconds in a day
// floor to round down, change to ceil to round up
$datediff = floor(($1date - $2date) / 86400);
$cost = $days * 10;
You could do it in MySQL then work it into a variable. Do the initial call
SELECT TIMESTAMPDIFF('Date1','Date2');
Try:
$days = date_diff(date_create($row['Date1']), date_create($row['Date2']))->format('%a');
$cost = $days * 10;

Categories