Compare two date time to check whether date is active PHP - php

I have one datetime in database which is saved in datetime format such as
2019-09-23 06:00:00
now i want to compare whether this datetime is greater than current datetime so as to check whether this record is of past or active for that i am doing something like this:
$currentDate = date('Y-m-d H:i:s');
$scheduleDate=date("Y-m-d H:i:s", strtotime($tableData->start));
if($scheduleDate < $currentDate)
{
echo "active";
}
else{
echo "Inactive";
}
This is database datetime :
2020-04-03 06:15:00
This is current datetime :
2020-04-03 17:50:00
Now i'm getting active but the my database datetime is older because it is of morning,

try to set your default time zone
eg: date_default_timezone_set("America/New_York");
then use below code
$currentdate1=strtotime($currentDate);
$scheduleDate1=strtotime($scheduleDate);
if($currentdate1 < $scheduleDate1)
{
echo "active";
}else
{
echo "Inactive";
}

Related

comparison of datetime value and system date time fails

i have the expiry date saved for each post using date time picker, now i need to check it against the current date and time each time aperson visits the page and display msg according to whether its expired ornot...but the comparison fails
i tried with date(),date create from format and DateTime::createFromFormat...but it fails the check if (is_a($expirydate, 'DateTime')).
$todaydate=new DateTime();
$todaydate->format('d/m/Y H:i:s');
$expirydate=strtotime(get_field('expirydate',$post->ID));
$expirydate=date('d/m/Y H:i:s', $expirydate);
if (is_a($todaydate, 'DateTime')) {
echo "today date is datetime";
if (is_a($expirydate, 'DateTime')) {
echo "expiry date check passed";
}}
if ($expirydate >= $todaydate)
{
echo "not expired";
}
it echos the msg today date is datetime but thats all...doesnt display msg "expiry date check passed" or "not expired"...its shud diplay all of the 3 messages.
Work with strtotime is Old School. Better use DateTime. DateTime objects can also be compared directly.
$expirydate=date_create(get_field('expirydate',$post->ID));
//$expirydate=date_create('today 11:19'); //for a test
$todaydate=new DateTime('Now'); //with current Time
//$todaydate=new DateTime('Today'); //with Time 00:00
if($todaydate > $expirydate) {
echo 'expired';
}

Comparing two dates in PHP and MYSQL

I want to compare two dates and time values in PHP. One date is coming from MySQL, and second one is the current date. I want to run some code when both dates are the same. I tried the code below, but condition satisfies any time which is wrong.
$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if($current_datetime == $send_date){
//I want to run some code here
}else{
}
What is wrong with the code? I also tried to covert both dates with strtotime() before comparing, but it gave me the same issue. The above condition satisfies any time even if both dates are different.
Try this :
$current_datetime = date('Y-m-d H:i');
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if(strtotime($current_datetime) == strtotime($send_date)){
//I want to run some code here
}else{
}
Hope it helps !!!!
One way is to fetch the Unix timestamp (seconds since '1970-01-01 00:00:00' UTC) from MySQL, then operate on the numbers:
$row = get_db_row("SELECT UNIX_TIMESTAMP(send_date) AS send_date_ts
FROM table WHERE $condition");
$hours = (int) ($row['send_date_ts'] / 3600);
$current_hours = (int) (time() / 3600);
if ($hours == $current_hours) {
// current hour
}
Timestamps are convenient because:
there is no need to take the format into account;
operations on numbers are usually faster;
the code looks cleaner.
Try this. On my server is working just great I've got something else because they aren't equal. Date which I receive from database is type datetime format 2015-04-13 09:03:49
<?php
$current_datetime = strtotime(date('Y-m-d H:i'));
$send_date = date("Y-m-d H:i", strtotime($row['send_date'])); // suppose $row['send_date']'s value is '2016-10-17 15:00'
if($current_datetime == $send_date){
//I want to run some code here
echo 'something';
}else{
echo 'something else';
}
Output:
echo $current_datetime . '<br/>';
2016-10-17 09:19
echo $send_date .'<br/>';
2015-04-13 09:03
// result
something else

match datetime with database datetime

I have a exam form. But I want to show that form within given date and time. Means if start date time is 2016-06-24 11:34:04 and end date time is 2016-06-27 00:00:00 If any merson open form between given date and time the form should be open otherwise it will go on another message page. I have a code .But it is not working. Please help.
<?php
include("connection.php");
date_default_timezone_set('Asia/Kolkata');
$timestamp = time();
$date_time = date("Y-m-d H:i:s", $timestamp);
$result=mysql_query("select * from activeform");
while($row=mysql_fetch_array($result))
{
extract($row);
if($date_time > '$startdate' and $date_time < '$enddate')
{
header("Location: myaccount.php");
}
else{
echo"date has been exceeds";
}
}
?>
You can use strtotime if you would like to compare dates in PHP. You may want to look at this answer

How to determine if date entered by the user is in future?

How do I know in PHP that the date entered by the user in future than the current date.
i.e
if($current_date<$future_date)
{
//do all the success actions here
}
else
{
//show the user that they have selected a date in the past.
}
Well you first need to convert the date string using strtotime() function and then check if future date value is greater than current date value.
Here is the code snippet:
$today_date=date('Y-m-d H:i:s');
$current_date=strtotime($today_date);
$future_date=strtotime($future_date);//retrieved from user's input
Now you can call your function:
if($current_date<$future_date)
{
//do all the success actions here
}
else
{
//show the user that they have selected a date in the past.
}
I suppose you know the date format in which your users enters their date cos it will help you understand what date format you will be working with. But irrespective of whatever date format you are working with, you can always convert it to a more suitable one using:
strtotime()
Below is an example of checking a future date (working strictly with date)
$currentDate = date("Y-m-d");
$userFututeDate = "2014-05-08";
if($userFututeDate > $currentDate){
echo 'You have selected a date in the future';
}
else{
echo 'You have selected a date in the past.';
}
Below is another example of checking a future date (working with date and time)
$currentDateTime = date("Y-m-d H:i:s");
$userFututeDateTime = "2014-05-07 05:05:08";
if($userFututeDateTime > $currentDateTime){
echo 'You have selected a date in the future';
}
else{
echo 'You have selected a date in the past.';
}
Perhaps, you need to understand how to play around with strtotime(), find some sample below:
$dateVar = "25-07-2014 05:05:08";
echo date("F j Y", strtotime($dateVar));
echo '<br><br>';
echo date("Y-m-d", strtotime($dateVar));
echo '<br><br>';
echo date("F jS Y, g:i a", strtotime($dateVar));
Check out for more on strtotime()

PHP Compare datetime values

In my PHP application I'm trying to compare date time values like the following:
if($datetime_from_db < date('Y-m-d H:i:s'))
{
// then do something
}
Both values are in the same format. What I can't figure out is why it only compares the date and ignores the time. Both the date and the time values are important for me but I don't know how to make it work.
Comparing a string like "2011-02-14 15:46:00" to another string doesn't actually compare dates, it compares two strings according string parsing numeric rules. You will need to compare actual numeric timestamps:
strtotime($datetime_from_db) < time()
If you want this to work with dates past 2038, you can't use strtotime() or time().
See this question for the explanation.
A better approach:
new DateTime($datetime_from_db) < new DateTime();
This may help you.
$today = date("m-d-Y H:i:s");
$thisMonth =date("m");
$thisYear = date("y");
$expectedDate = $thisMonth."-08-$thisYear 23:58:00";
//pr($today);
//pr($expectedDate);
if (strtotime($expectedDate) > strtotime($today)) {
echo "Expected date is greater then current date";
return ;
} else
{
echo "Expected date is lesser then current date";
}
Here is a solution where we use strtotime. I give two examples.
First one comparing the whole timestamp. Second one is just compare the date.
<?php
$date = "2022-10-06 17:49:10"; // string. can set any current timestamp
#example 1 - compare the date and time Y-m-d H:i:s
if(date("Y-m-d H:i:s" , strtotime($date)) >= date("Y-m-d H:i:s")){
echo "the date checked is bigger than today";
}else{
echo "the date checked is smaller than today";
}
#example 2 - compare the date only Y-m-d
if(date("Y-m-d" , strtotime($date)) == date("Y-m-d")){
echo "same day is true";
}else{
echo "same day is false";
}

Categories