Solved

Show content if 30 days ahead of TIME_STAMP date

Posted on 2008-06-15
12
1,141 Views
Last Modified: 2013-12-12
Hi,

I need to show some content based on the length of time 'away' from TIME_STAMP issued by MySQL.

Is there a simple way to achieve this via PHP, the logic might be something like :

$d = (TIME_STAMP)

if $d >= 30 day's ahead of $d { some content } else { other content }

???
0
Comment
Question by:wilson1000
  • 4
  • 3
  • 3
  • +1
12 Comments
 
LVL 3

Expert Comment

by:Fapiko
ID: 21790325
I forget how MySQL's timestamp works, but here is how to do it. First, parse the timestamp returned from the sql query.

$d = date_parse($mysqlTimestamp);
$d = mktime($d->hour, $d->minute, $d->second, $d->month, $d->day, $d->year);

Now, create a timestamp for 30 days from today.
$todayPlus30 = time() - 2592000;

Finally, compare
if ($d >= $todayPlus30) echo 'Sql Content';
else echo 'Other content';
0
 
LVL 3

Expert Comment

by:Fapiko
ID: 21790329
By the way, you can also compare the date in the SQL statement, but I'm not going to get into that unless you request it. The above code is relying on MySQL returning a timestamp in a format date_parse can parse, which I believe it does.

Also, change $todayPlus30 = time() - 2592000; to $todayPlus30 = time() + 2592000;
I acidently used a subtraction sign instead of addition.
0
 
LVL 18

Expert Comment

by:Matthew Kelly
ID: 21790341
See the code below. Let me know if you have any questions.
<?php
 

$timestamp = "2008-09-16 00:00:00";
 

// first argument is days to test, second argument is timestamp

if ( checkDateIsGreater(30,$timestamp) )

{

	echo $timestamp." is more than 30 days away";

}

else 

{

	echo $timestamp." is less than 30 days away";

}
 

//outputs time in mm/dd/yyyy  HH:mm format

function timeformat($mysql_datetime_str)

{

	$retstrg="";

	// MYSQL 4.0

    	if (strlen($mysql_datetime_str) == 14)

    	{

		$year = substr($mysql_datetime_str, 2, 2);

		$month = substr($mysql_datetime_str, 4, 2);

		$day = substr($mysql_datetime_str, 6, 2);

		$hour = substr($mysql_datetime_str, 8, 2);

		$minute = substr($mysql_datetime_str, 10, 2);

		$second = substr($mysql_datetime_str, 12, 2);

		$retstrg= mktime($hour, $minute, $second,$month,$day,$year);

    	} 

	// MYSQL 5.0 and 4.1

	else if (strlen($mysql_datetime_str) == 19) 

	{

		$year = substr($mysql_datetime_str, 0, 4);

		$month = substr($mysql_datetime_str, 5, 2);

		$day = substr($mysql_datetime_str, 8, 2);

		$hour = substr($mysql_datetime_str, 11, 2);

		$minute = substr($mysql_datetime_str, 14, 2);

		$second = substr($mysql_datetime_str, 17, 2);

		$retstrg= mktime($hour, $minute, $second,$month,$day,$year);

	}

	else if ( strlen($mysql_datetime_str) == 21 )

	{

		$year = substr($mysql_datetime_str, 0, 4);

		$month = substr($mysql_datetime_str, 4, 2);

		$day = substr($mysql_datetime_str, 6, 2);

		$hour = substr($mysql_datetime_str, 8, 2);

		$minute = substr($mysql_datetime_str, 10, 2);

		$second = substr($mysql_datetime_str, 12, 2);

		if ( $minute >= "60" ) 

		{ 

			$minute = "00"; 

			if ( $hour == "00" ) { $hour = "01"; }

			else if ( $hour == "01" ) { $hour = "02"; }

			else if ( $hour == "02" ) { $hour = "03"; }

			else if ( $hour == "03" ) { $hour = "04"; }

			else if ( $hour == "04" ) { $hour = "05"; }

			else if ( $hour == "05" ) { $hour = "06"; }

			else if ( $hour == "06" ) { $hour = "07"; }

			else if ( $hour == "07" ) { $hour = "08"; }

			else if ( $hour == "08" ) { $hour = "09"; }

			else if ( $hour == "09" ) { $hour = "10"; }

			else if ( $hour == "10" ) { $hour = "11"; }

			else if ( $hour == "11" ) { $hour = "12"; }

			else if ( $hour == "12" ) { $hour = "13"; }

			else if ( $hour == "13" ) { $hour = "14"; }

			else if ( $hour == "14" ) { $hour = "15"; }

			else if ( $hour == "15" ) { $hour = "16"; }

			else if ( $hour == "16" ) { $hour = "17"; }

			else if ( $hour == "17" ) { $hour = "18"; }

			else if ( $hour == "18" ) { $hour = "19"; }

			else if ( $hour == "19" ) { $hour = "20"; }

			else if ( $hour == "20" ) { $hour = "21"; }

			else if ( $hour == "21" ) { $hour = "22"; }

			else if ( $hour == "22" ) { $hour = "23"; }

			else if ( $hour == "23" ) { $hour = "00"; }

		}

		if ( $second >= "60" ) { $second = "00"; }

    		$retstrg= mktime($hour, $minute, $second,$month,$day,$year);

	}

	return $retstrg;

}
 

function fnc_date_calc($this_date,$num_days)

{

	$my_time = strtotime ($this_date); //converts date string to UNIX timestamp

	$timestamp = $my_time + ($num_days * 86400); //calculates # of days passed ($num_days) * # seconds in a day (86400)

	$return_date = date("Y-m-d H:i:s",$timestamp+86400);  //puts the UNIX timestamp back into string format

	return $return_date;//exit function and return string

}//end of function
 

function checkDateIsGreater($numDaysToAdd,$timeStamp)

{

	// subtract 30 days from the timestamp passed

	$dateToTest = timeformat(fnc_date_calc($timeStamp,$numDaysToAdd*-1));

	$currentDate = timeformat(date("Y-m-d H:i:s"));
 

	// if current date is greater that timestamp minus 30 days return false

	// if current date is less than timestamp minus 30 days return true

	if ( $currentDate < $dateToTest) { return true; }

	else { return false; }

}

?>

Open in new window

0
 
LVL 18

Expert Comment

by:Matthew Kelly
ID: 21790363
The way to do it right in SQL would be to make to SQL selects.

First make :

$sql = "SELECT * FROM `test` WHERE `time` > DATE_ADD( NOW( ) , INTERVAL 30 DAY )";

and then:

$sql = "SELECT * FROM `test` WHERE `time` <= DATE_ADD( NOW( ) , INTERVAL 30 DAY )";

I didn't test those SQL statements but it would be something like that
0
 
LVL 3

Expert Comment

by:Fapiko
ID: 21791282
Wow, I was not thinking when I wrote my first post. Here it is, fixed. It does pretty much the same thing mathewstevenkelly's code does, except it uses a function which already exists in PHP to replace about sixty lines of his code...
// This would be set from your MySQL query, of course

$mysqlTimestamp = "2008-06-12 03:01:02";
 

// Create a UNIX timestamp from these values

$d = date_parse($mysqlTimestamp);

$d = mktime($d['hour'], $d['minute], $d['second'], $d['month'], $d['day'], $d['year']);
 

// Now, create a timestamp for 30 days from today. 2592000 is the number of seconds in 30 days.

$todayPlus30 = time() + 2592000;
 

// Finally, compare to see if the post is old enough

if ($d >= $todayPlus30) echo 'Sql Content';

else echo 'Other content';

Open in new window

0
 
LVL 3

Accepted Solution

by:
Fapiko earned 250 total points
ID: 21791286
And fixed yet again.. forgot to close a quote.
// This would be set from your MySQL query, of course

$mysqlTimestamp = "2008-06-12 03:01:02";

 

// Create a UNIX timestamp from these values

$d = date_parse($mysqlTimestamp);

$d = mktime($d['hour'], $d['minute'], $d['second'], $d['month'], $d['day'], $d['year']);

 

// Now, create a timestamp for 30 days from today. 2592000 is the number of seconds in 30 days.

$todayPlus30 = time() + 2592000;

 

// Finally, compare to see if the post is old enough

if ($d >= $todayPlus30) echo 'Sql Content';

else echo 'Other content';

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 250 total points
ID: 21791356
When calculating today+30 days, use:

$todayPlus30 = date('Y-m-d H:i:s', strtotime('+30 Days'));

Then use that value in your query. I think it is slightly more efficient than the mysql approach that matthewstevenkelly proposes. I think mysql will try and recalculate the date_add for each row, unless they have finally optimized their query parser.

Kind regards

-r-
0
 
LVL 4

Author Comment

by:wilson1000
ID: 21792929
Thank you gentlemen, i'm not in the office at the moment. I'll test it when I return - I love the weight of Fapiko's edition.

Many thanks, speak later
0
 
LVL 4

Author Comment

by:wilson1000
ID: 21796407
Gents, i'm having a problem trying to figure out the logic.

if $d is greater than or equal to $todayPlus30 {show content}

Well... $d is always going to be less than $todayPlus30 because $todayPlus30 is 30 days ahead.

Am I missing something?

Many thanks
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21796475
That depends on what the value of $d is.
0
 
LVL 4

Author Comment

by:wilson1000
ID: 21796478
Based on your suggestions, I think i've nailed it...

Let me know your thoughts

Thanks again
$ts = $row_WADAorders['OrderDate'];

$nt = date('Y-m-d H:i:s', strtotime('-30 Days'));
 

  if ($ts >= $nt) {show this;}else{show this;}

Open in new window

0
 
LVL 49

Assisted Solution

by:Roonaan
Roonaan earned 250 total points
ID: 21796553
I think you are fine.

One change might be to leave out the time when determining $nt. Not sure what you prefer on that situtaion.

$nt = date('Y-m-d', strtotime('-30 Days')).' 00:00:00';
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now