Confusion about sequences and how php/mysql handles repeat regions

Posted on 2009-05-15
Last Modified: 2013-12-12
I am attempting a large php coding project with the hopes of learning more about php, but I'm running into a snag. I think I'm thinking about this the wrong way, and probably have my sequential order wrong in the code. I'm hoping you all can help me straighten this out in my head.

Here's what I want to happen:

A function (date_diff) compares two dates (one from a MySQL table and today's date).
Once it has the difference, I want to only show the table results if the amount of days ($ndays) is less than 15 (two weeks), for record #1.
The action then repeats itself, with record #2.

Currently, it's all or nothing. If one date_diff is less than 15, all entries show.

Sample output:
new_datecar: 2009-07-20
ndays: 66
task: Task A

new_datecar: 2009-05-20
ndays: 5
task: Task K

new_datecar: 2009-10-20
ndays: 158
task: Task M

How do I handle a situation like this where I want a statement to only test one table row at a time, or is there a syntax problem that's causing this to happen? I've included my code below to, hopefully, make what I'm asking clearer.
<?php require_once('Connections/mhc.php'); ?>


if (!function_exists("GetSQLValueString")) {

function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")


  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;


  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);


  switch ($theType) {

    case "text":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";


    case "long":

    case "int":

      $theValue = ($theValue != "") ? intval($theValue) : "NULL";


    case "double":

      $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";


    case "date":

      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";


    case "defined":

      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;



  return $theValue;



mysql_select_db($database_mhc, $mhc);

$query_taskscar = "SELECT tasks_user_1547.task_id, tasks_user_1547.`update`,,, tasks.task_id, tasks.task, tasks.freq FROM tasks_user_1547, tasks WHERE = 'Y' AND tasks.task_id = tasks_user_1547.task_id AND = 'car'";

$taskscar = mysql_query($query_taskscar, $mhc) or die(mysql_error());

$row_taskscar = mysql_fetch_assoc($taskscar);

$totalRows_taskscar = mysql_num_rows($taskscar);


function date_diff($str_start, $str_end)



$str_start = strtotime($str_start); // The start date becomes a timestamp

$str_end = strtotime($str_end); // The end date becomes a timestamp


$nseconds = $str_end - $str_start; // Number of seconds between the two dates

$ndays = round($nseconds / 86400); // One day has 86400 seconds

$nseconds = $nseconds % 86400; // The remainder from the operation

$nhours = round($nseconds / 3600); // One hour has 3600 seconds

$nseconds = $nseconds % 3600;

$nminutes = round($nseconds / 60); // One minute has 60 seconds

$nseconds = $nseconds % 60;

echo "ndays: $ndays";

echo '<br>';




<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="">


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Untitled Document</title>




<?php do { ?>



					//tests to see if the last change value is empty (therefore never set)

					if (isset($row_taskscar['update'])) {


						$thisday = date("Y-m-d");



						//sets the last change date to a variable $date

						$datecar = $row_taskscar['update']; 

						//format the date for mktime() function -- you need day, month, year

						$yearcar = substr($datecar, 0,4); 

						$monthcar = substr($datecar, 5, 2); 

						$daycar = substr($datecar, 8, 2); 

						//sets the frequency to a variable $freq

						$freqcar = $row_taskscar['freq'];

						//sets the variable to hold the new date. Calculates the new date by 

						//evaluating the lastchange date and adding the frequency amount measured in months

						$new_datecar = date("Y-m-d", mktime(0, 0, 0, $monthcar +$freqcar, $daycar , $yearcar)); 

						echo "new_datecar: $new_datecar"; 

						echo '<br>';

						date_diff("$thisday", "$new_datecar");


							global $ndays;

							if ($ndays <= 15) {

							echo 'task: ';

							echo $row_taskscar['task'];			  


						echo '<br><br>';



  <?php } while ($row_taskscar = mysql_fetch_assoc($taskscar)); ?>






Open in new window

Question by:kentcommunications
  • 4
  • 4
LVL 108

Expert Comment

by:Ray Paseur
ID: 24399296
Let me try to paraphrase...

I want to show only the rows when the date in the row is less than two weeks from today?

In that case, you would use a SELECT statement to create a record set with all those rows, and only those rows.  So the PHP / MySQL would be something like this
$future_date = date('c', strtotime('+2 weeks'));

$sql = "SELECT fields FROM table WHERE row_date <= \"$future_date\"";

Open in new window


Author Comment

ID: 24412884
Thanks for your reply. Unfortunately, this (much easier) way doesn't work with the way the app is set up.

Currently, the user can see a list of tasks they have available. When they complete a task, they click a button and it updates the database with the date it was completed. The current system displays all tasks, whether they are due or not.
I'm trying to send users an e-mail once a month giving them a list of tasks they have almost due. In the table column, update, it is storing the last time they completed it--not the next time it's due. The php code is processing when the item is due by adding the "update" date to the "frequency" (from another table) to decide whether it's due or not. Not all items have the same frequency.
LVL 108

Accepted Solution

Ray Paseur earned 500 total points
ID: 24412986
I'm sure that I am not understanding the application design correctly.  But try adding this statement to the date_diff function after line 40

global $ndays;

Also, you might want to add this error_reporting() statement at the top of your scripts when you are debugging.  It would show you that the $ndays variable is undefined in the main script because it was not declared to be global in the function scope.


HTH, ~Ray

Author Comment

ID: 24413537
That fixed it :)

I do have a quick question about globals, just to be sure.
Do you only have to put "global" next to the original statement of the $var for it to become a global variable? As a test, I removed the "global" part from line 94 and the code still works.
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 500 total points
ID: 24415648
Global variables are made "global" inside functions, then they are available to the main script outside of the function (but not inside other functions).  There are a couple of ways to look at this sort of thing.  One is called variable "scope"...

Another way is to put the values into $GLOBALS.  Learn about the "superglobal" variables here:

In object oriented programming you can explicitly declare variables to be public, private, protected, static, etc.  They are called "properties" in OOP.  More here:

HTH, ~Ray

Author Comment

ID: 24416065
Great, thanks for all the help!

Author Closing Comment

ID: 31582089
Excellent help again, thanks :)
LVL 108

Expert Comment

by:Ray Paseur
ID: 24416077
Thanks for the points - glad to be of assistance! ~Ray

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 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

10 Experts available now in Live!

Get 1:1 Help Now