Solved

Show content if 30 days ahead of TIME_STAMP date

Posted on 2008-06-15
12
1,140 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction Many web sites contain image galleries; a common design for these galleries includes a page with a collection of thumbnail images.  You can click on each of the thumbnail images to see the larger version of the image.  This is easily i…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

747 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

17 Experts available now in Live!

Get 1:1 Help Now