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

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.';
}

Related

How to check expiry date is passed with current date in php? [duplicate]

This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 4 years ago.
I have to check expiry date is passed with current date and then disable the link. Should i convert strtotime function and check or is there any simple way to check it. We should not compare the dates
$current_date = "2018-07-23 12:00:00";
$expiry date = "2018-07-22 12:00:00";
if($expiry date>$current_date){
$link = "<a href="http://www.example.com";
} else {
$link = "#";
}
I would use strtotime() as follows:
$current_date = "2018-07-23 12:00:00";
$expiry date = "2018-07-22 12:00:00";
if (strtotime($expiry date) > strtotime($current_date)) {
$link = "<a href="http://www.example.com";
} else {
$link = "#";
}

PHP date comparison between two dates [duplicate]

This question already has answers here:
How to calculate days between two dates in PHP?
(6 answers)
Closed 8 years ago.
I want to keep track the status if there is no shipment of product within 2 days then there is must have a notification to alert them by email to do a shipment. The shipment date can be fetch from email date but it is possible if I compare :
//MAX(date_shipment = latest date record of shipment in database
if (MAX(date_shipment) - dateNow() >= 2 days )
{
$update ="update table1 set remarks = 'No shipment received since 2 days ago', status = 'warning'";
$result = mysql_query($update);
}
else
{ }
But there is problem I thinks because this code is compare with dateNow(). How come if there is new shipment received, still the date comparison code running but not compare with this new shipment date received. I have no idea how to explain and how to start to do this function.
Can someone advise what is the right way to solve this prob? Thanks in advance
Let's try this code:
$currentDate = getdate();
$anotherDate = MAX(date_shipment); //whatever this is
//this will give sql format to the current date
$year = $currentDate['year'];
$month = $currentDate['mon'];
$day = $currentDate['mday'];
$currentDate = $year.'-'.$month.'-'.$day;
$date1 = date_create($anotherDate); //assuming your date has sql format
$date2 = date_create($currentDate);
$difference = date_diff($date1, $date2);
$difference = str_replace(" days","",$difference->format('%R%a days'));
if ($difference >= 2){
$update ="update table1 set remarks = 'No shipment received since 2 days ago', status = 'warning'";
$result = mysql_query($update);
}
else
{ } //I don't get what you want here
This seems like what you are trying to do. Do not use mysql anymore, use mysqli.
$dbDate = mysql_query("SELECT `date_shipment` FROM table1");
$addedDate = new DateTime();
$addedDate->modify('+2 day');
if ($dbDate['date_shipment'] >= $addedDate)
{
$update ="UPDATE table1 SET remarks = 'No shipment received since 2 days ago',
status = 'warning'";
$result = mysql_query($update);
}
else
{ }

calculate the difference between two dates with strtotime [duplicate]

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>';
}

How do I check if this date format is in the future? [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.
My date is being returned like 01/05/2013 12:44. How do I check in php if this datetime is in the future?
Try this:
<?php
$dt = '01/05/2013 12:44';
$nowdt = time();
$diff = strtotime($dt) - $nowdt;
echo $diff;
if($diff > 0){
echo (" your date is future date");
} else {
echo ("your date is is not future date");
}
?>
You can compare time() with the UNIX timestamp representation of that date:
if (time() < strtotime("01/05/2013 12:44"))
{
// your time is greater than todays date
}
Another way
$time = strtotime(str_replace("/","-",$date) )
if ($time > time())
{
echo "$date is in the future."
}
From the PHP manual use mktime.
Example in pseudo code:
$today = mktime("today");
$your_date = mktime("...");
if ($your_date > $today) {
// it's in the future
}
Convert to Unix time, then see if it's greater than the current time.
<?php
if(strtotime("01/05/2013 12:44") > time()) {
//time is in the future.
}else{
//time is in the past
}
?>

Difference of hours between two dates (integer) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calculate number of hours between 2 dates in PHP
How can I get the number of hours of difference between two dates in PHP? I need to get an integer since I want to know if is bigger or smaller than a particular value.
Try:
$date1 = "2012-11-05 12:35:00";
$date2 = "2012-11-07 14:35:00";
$diff = strtotime($date2) - strtotime($date1);
$diff_in_hrs = $diff/3600;
print_r($diff_in_hrs);
Manual
Demo
If you have an up-to-date PHP
$dateOne = new DateTime('2012-01-20 00:00:00');
$dateTwo = new DateTime('2012-01-21 02:00:00');
// Procedurally
$interval = date_diff($dateOne, $dateTwo);
// Alternatively OOP style if supported
$interval = $dateOne->diff($dateTwo);
See: http://www.php.net/manual/en/class.dateinterval.php
<?php
$time1 = time();
$time2 = mktime(0,0,0,11,13,2012); // earlier today
echo ($time1 - $time2) / 3600; // 3600 seconds in hour
?>

Categories