Jump to content

Problem with date_add & date_sub


Kaliphornia

Recommended Posts

Hi all,

 

I'm a PHP coder who sucks at date manipulation. Here's the problem:

 

I have code that builds and manipulates a bunch of dates, starting from 2 "main" date values.

 

Then it changes the main dates via date_add & date_sub. It works just fine if all I want to do is print the date values inside a "while" loop. See example here:http://gardencalc.drivingpeace.com/results1.php

 

However, now I need to load everything into an array and pass it to a grid widget. Date_add & date_sub appear to change not only the orginal "main" date values, but also the values of any other variables created from the original date, so all date values become the same.

See grid widget example here: http://gardencalc.drivingpeace.com/grid.htm

 

I need to somehow change the dates inside some logic code, capture the saved changes into the array, then pass the array to the widget. Like I said, I suck at date functions, and I need an alternative to date_add, etc.

 

PHP code for the grid widget is below. Any suggestions are most welcome. I imagine there's an easy answer, but I'm stumped!

 

Thanks!

<?php
  $loc_id = 174;
  $prob_id = 10;
  #Include the connect.php file
  include('connect.php');
  #Connect to the database
  //connection String
  $connect = mysql_connect($hostname, $username, $password)
  or die('Could not connect: ' . mysql_error());
  //select database
  mysql_select_db($database, $connect);
  //Select The database
  $bool = mysql_select_db($database, $connect);
  if ($bool === False){
  print "can't find $database";
  }

  $tempSQL = "SELECT * FROM prob_28 WHERE prob_id=$loc_id";

    $temp = mysql_query($tempSQL) or die(mysql_error());

    while($row = mysql_fetch_array( $temp )) {
        $prob1_90 = $row['prob1_90'];
        $prob1_50 = $row['prob1_50'];
        $prob1_10 = $row['prob1_10'];
        $prob2_10 = $row['prob2_10'];
        $prob2_50 = $row['prob2_50'];
        $prob2_90 = $row['prob2_90'];
    }

    //build the dates
    if ($prob_id == "10"){
        $first_frost_date = date_create(date("Y").'-'.substr($prob1_10,0,2).'-'.substr($prob1_10,2,2));
        $last_frost_date = date_create(date("Y").'-'.substr($prob2_10,0,2).'-'.substr($prob2_10,2,2));
    }elseif ($prob_id == "50"){
        $first_frost_date = date_create(date("Y").'-'.substr($prob1_50,0,2).'-'.substr($prob1_50,2,2));
        $last_frost_date = date_create(date("Y").'-'.substr($prob2_50,0,2).'-'.substr($prob2_50,2,2));
    }else{
        $first_frost_date = date_create(date("Y").'-'.substr($prob1_90,0,2).'-'.substr($prob1_90,2,2));
        $last_frost_date = date_create(date("Y").'-'.substr($prob2_90,0,2).'-'.substr($prob2_90,2,2));
    }


  // get data and store in a json array
  $query = "SELECT * FROM data ORDER BY data_id";
  $result = mysql_query($query) or die("SQL Error 1: " . mysql_error());
  while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

            $curcrop = array();
            $data_id = $row['data_id'];
            $days_to_harvest = $row['days_2_harvest'];
            $resolution = $row['resolution'];

            //Get type name
            $type_id = $row['type_id'];
            $type = mysql_query("SELECT * FROM type WHERE type_id=$type_id") or die(mysql_error());

            while($typerow = mysql_fetch_array( $type )) {
                $curcrop['Type'] = $typerow['type_name'];
            }

            //Get planting method name
            $method_id = $row['method_id'];

            $method = mysql_query("SELECT * FROM method WHERE method_id=$method_id") or die(mysql_error());

            while($methodrow = mysql_fetch_array( $method )) {
                $curcrop['Method'] = $methodrow['method_name'];
            }

            //Get crop name
            $crop_id = $row['crop_id'];

            $crop = mysql_query("SELECT * FROM crop WHERE crop_id=$crop_id") or die(mysql_error());

            while($croprow = mysql_fetch_array( $crop )) {

                $curcrop['Crop'] = $croprow['crop_name'];
            }

            //calculate the direct sow date
            //Only build real date if the planting method is direct sow
            if ($method_id == 1){
                //check if it's a fall or spring crop type
                //spring date
                if ($type_id == 1){
                    $direct_sow_date = $first_frost_date;
                    $date_int = $row['wks_b4_frost_2_sow'] * 7;
                    date_sub($direct_sow_date,date_interval_create_from_date_string("$date_int days"));
                //fall date
                }else{
                    $direct_sow_date = $last_frost_date;
                    $date_int = $row['wks_b4_first_frost'] * 7;
                    date_sub($direct_sow_date,date_interval_create_from_date_string("$date_int days"));
                }
            }else{
                $direct_sow_date = null;
            }


            $curcrop['SowDirect'] = $direct_sow_date;



            //calculate transplant date
            if ($method_id == 2){
                //check if it's a fall or spring crop type
                //spring date
                if ($type_id == 1){
                    $transplant_date = $first_frost_date;
                    $date_int = $row['wks_b4_last_frost_2_trans'] * 7;
                    date_sub($transplant_date,date_interval_create_from_date_string("$date_int days"));
                //fall date
                }else{
                    $transplant_date = $last_frost_date;
                    $date_int = $row['wks_b4_first_frost'] * 7;
                    date_sub($transplant_date,date_interval_create_from_date_string("$date_int days"));
                }
            }else{
                $transplant_date = null;
            }

            //transplant sow date
            if ($transplant_date != null){
                $date_int_trans = $row['wks_2_grow_trans'] * 7;
                date_sub($transplant_date,date_interval_create_from_date_string("$date_int_trans days"));
                $curcrop['SowTransplant'] = $transplant_date;
                date_add($transplant_date,date_interval_create_from_date_string("$date_int_trans days"));
            }else{
                $curcrop['SowTransplant'] = null;
            }


            $curcrop['Transplant'] = $transplant_date;


            //begin harvest date

            if ($method_id == 1 && $direct_sow_date != null){
                date_add($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
                date_sub($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
                $curcrop['BeginHarvest'] = $direct_sow_date;
                date_sub($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
                date_add($direct_sow_date,date_interval_create_from_date_string("$resolution days"));

            }

            if ($method_id == 2 && $transplant_date != null){
                date_add($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
                date_sub($transplant_date,date_interval_create_from_date_string("$resolution days"));
                $curcrop['BeginHarvest'] = $transplant_date;
                date_sub($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
                date_add($transplant_date,date_interval_create_from_date_string("$resolution days"));

            }

            //end harvest date
            if ($method_id == 1 && $direct_sow_date != null){
                date_add($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
                date_add($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
                $curcrop['EndHarvest'] = $direct_sow_date;
                date_sub($direct_sow_date,date_interval_create_from_date_string("$days_to_harvest days"));
                date_sub($direct_sow_date,date_interval_create_from_date_string("$resolution days"));
                date_add($direct_sow_date,date_interval_create_from_date_string("$date_int days"));

            }

            if ($method_id == 2 && $transplant_date != null){
                date_add($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
                date_add($transplant_date,date_interval_create_from_date_string("$resolution days"));
                $curcrop['EndHarvest'] = $transplant_date;
                date_sub($transplant_date,date_interval_create_from_date_string("$days_to_harvest days"));
                date_sub($transplant_date,date_interval_create_from_date_string("$resolution days"));
                date_add($transplant_date,date_interval_create_from_date_string("$date_int days"));

            }


        $crops[] = $curcrop;

  }



  echo json_encode($crops);
?>

grid_data1.php

Link to comment
Share on other sites

you are using php datetime objects and the date_add/date_sub functions/methods are modifying the instance of the datetime object. these are not values, they are instances of a class/object.

 

it's also a safe bet that your grid widget expects an array of literal date strings. to output or store to an array entry, the current value of your datetime object as a literal string, you need to use the date_format() function/method.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.