Jump to content

Check Date/Time in PHP


GreenSmurf

Recommended Posts

What I need the code to check is that $reQuest is 24 hours in the past then return a time based on how long ago that was in the past if 24 hours has not passed.

if ($reQuest!=""){
		if (is_on_interval($phpdate,$getdate,24)==false){ //24 hours has not passed.
			$timePass=false;
			//echo $reQuest."<br />"; //debug lines of code
			//echo phpversion(); // Installed 5.3.1 but still registered as 5.2.2 --odd
			$start = new DateTime($reQuest); //First or start date to be check as pulled from the database.
			$time = $start->diff(new DateTime(date("Y-m-d H:i:s", $phptime))); //Check the difference in date with diff() available in 5.3.1
		}else{$timePass=true;}
	}

 

The problem is that 5.3.1 did not install properly and being such I am trying to figure out a new way to get the difference in time from the DateTime in the database and the current time obtained by PHP. The

 $reQuest

variable has a value of 2010-02-18 12:47:15

Any help would be appreciated. Thank you.

 

-GS

Link to comment
https://forums.phpfreaks.com/topic/192558-check-datetime-in-php/
Share on other sites

if you do UNIX_TIMESTAMP(db_date_column) in your query you can turn the date value into a unix timestamp. then check that against time() - 60*60*24 (24 hrs ago)

 

I was trying to avoid the UNIX_TIMESTAMP because of the date limitations placed upon it. Plus, I am not trying to limit the query anyway because I am accessing other information in the table from the database. I need to compare the m in PHP.

not only before 1970

but after 2038

Thank you. You beat me to it. The issue is more that I just want to compare the dates. I thought I was on to something with this little bit of code but I have again hit a wall.

$mysqldate = date($reQuest, $phpdate);
		$phpdate = strtotime($mysqldate);

Any more ideas or code snippets would be appreciated.

 

-GS

I see the limitations but your still converting the mysql date to a unix timestamp in that code. whats the difference if you do it in mysql or php?

 

Got it. Its messy but the functionality is there:

if ($reQuest!=""){
		$timeSub = date("h:i A", strtotime('+1 day +1 minute '.$reQuest));
		$timeNow = date("Y-m-d H:i:s", time());
		$timeNow = strtotime($timeNow);
		//echo $timeNow."<br />";
		//echo $reQuest."<br />";
		$timeComp = date("Y-m-d H:i:s", strtotime('+1 day '.$reQuest));
		$timeComp = strtotime($timeComp);
		//echo $timeComp."<br />";
		if ($timeNow <= $timeComp){
			$timePass=false;
		}else{$timePass=true;}

Hope this helps anyone else who might need this sort of calc.

 

-GS

wow. you can definitely shorten that down.

 

you're still limited by unix timestamp constraints using that code so I don't see why you don't just do it in mysql.

 

use UNIX_TIMESTAMP to convert the value from the db in your query. if you need the mysql datestamp for something else just pull both values.

 

if($db_unix_time < (time() - 86400)) return true; else return false;

 

wow. you can definitely shorten that down.

 

you're still limited by unix timestamp constraints using that code so I don't see why you don't just do it in mysql.

 

use UNIX_TIMESTAMP to convert the value from the db in your query. if you need the mysql datestamp for something else just pull both values.

 

if($db_unix_time < (time() - 86400)) return true; else return false;

 

You are right about PHP doing the calculation in UNIX_TIMESTAMP but at least it is not in the DB for now and I just need to find a better solution to avoid the UNIX_TIMESTAMP.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.