Debug this date extract script

can you debug:

 $dateStr = explode(" ", $dateStart4);

  $dateStr2 = str_replace("/", "|", $dateStr[0]);

  $dateStr3 = explode("|", $dateStr2);

  $dateStart1 = $dateStr3[2]."-".$dateStr3[0]."-".$dateStr3[1];

  $dateStr = explode(" ", $dateEnd4);

  $dateStr2 = str_replace("/", "|", $dateStr[0]);

  $dateStr3 = explode("|", $dateStr2);

  $dateEnd1 = $dateStr3[2]."-".$dateStr3[0]."-".$dateStr3[1];

  echo $dateStart1.",".$dateEnd1;

  $data = new RecordPayPalBooking($name,$email,$phone,$comments,$country,$price,$discount,$days,$mobile,$dateStart1,$dateEnd1,$People,$agree,$propNo,$orderID,$paypal_name,$paypal_email,$paypal_amount);

  $tr1 = $data->exeSQLquery();

Error!
Notice: Undefined offset: 2 in /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php on line 179

Notice: Undefined offset: 1 in /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php on line 179

Notice: Undefined offset: 2 in /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php on line 186

Notice: Undefined offset: 1 in /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php on line 186
-02/14/2020-,-02/26/2020-Error: SQLSTATE[22007]: Invalid datetime format: 1292 Incorrect date value: ‘-02/14/2020-’ for column ‘dateStart’ at row 1!

but here simple file all OK

getting the correct: // 2020-02-13 desired

<?php
function convertYYYY_MM_DD($t)
{
  
  $dateStr = explode(" ", $t);
  $dateStr2 = explode("/", $dateStr[0]);
  
  return $dateStr2[2]."-".$dateStr2[0]."-".$dateStr2[1];


}
$dateStart1 = convertYYYY_MM_DD("02/13/2020 12:00 AM");
echo $dateStart1; // 2020-02-13

Try this:

<?php 
declare(strict_types=1);

$date1 = '02/13/2020 04:00 AM'; 

echo '<br>timestamp() ==> ' .$tmp = strtotime($date1);
echo '<br>';

$aDates = [
  'd-m-y',
  'd/m/y',
  'D-m-Y',
  'd-M-y',
  'D-M-Y',
  'Y-m-d',
  'Y-d-m',
];
foreach($aDates as $key => $date) :
  echo '<br>$date ==> ' .$date .' ==> ' .date($date,$tmp);
endforeach;  

Output:

Worth pointing out there are numerous ways to accomplish this, especially if you know the incoming format.

$datein = '02/13/2020 04:00 AM';
$dateout = DateTime::createFromFormat("m/d/Y h:i:s A",$datein);
$dateout->format("Y-m-d");

EDIT: At least, it would if you actually told it to use the date, Marc…

Which lines are these, in the code you showed? What data are you sending into the script when you get the error messages?

what this do>>> ?

declare(strict_types=1);

$dateStr = explode(" ", $dateStart4);

  $dateStr2 = str_replace("/", "|", $dateStr[0]);
  $dateStr3 = explode("|", $dateStr2);
  $dateStart1 = $dateStr3[2]."-".$dateStr3[0]."-".$dateStr3[1];  // line 179


  $dateStr = explode(" ", $dateEnd4);

  $dateStr2 = str_replace("/", "|", $dateStr[0]);
  $dateStr3 = explode("|", $dateStr2);
  $dateEnd1 = $dateStr3[2]."-".$dateStr3[0]."-".$dateStr3[1];  // 186 line

this required???

It is an PHP extra validation that is not essential but does highlight parameter type anomalies.

1 Like

without
declare(strict_types=1);
getting
Error! true
no write in db table
without getting - no write in db table
Error!
Fatal error: Uncaught TypeError: date() expects parameter 2 to be integer, boolean given in /home/polislatchiaptsv/public_html/main/data/RecordPayPalBooking.php:22 Stack trace: #0 /home/polislatchiaptsv/public_html/main/data/RecordPayPalBooking.php(22): date(‘Y-m-d’, false) #1 /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php(190): RecordPayPalBooking->__construct(‘777777777777777…’, ‘leonidas.e.savv…’, ‘777777777777777…’, ‘Notes\n---------…’, ‘Greece’, ‘1755’, ‘0’, ‘13’, ‘777777777777777…’, ‘02/14&#x2F…’, ‘02/27&#x2F…’, ‘777777777777777…’, ‘1’, ‘7’, ‘5FV47683TK31403…’, ‘Leonidas Savvid…’, ‘a080725@cytanet…’, ‘0.01’) #2 /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php(209): bookProcess(‘777777777777777…’, ‘leonidas.e.savv…’, ‘777777777777777…’, ‘Notes\n---------…’, ‘Greece’, ‘1755’, ‘0’, ‘13’, ‘777777777777777…’, ‘02/14&#x2F…’, ‘02/27&#x2F…’, ‘777777777777777…’, ‘1’, ‘Irene Villa \xE2\x80\x94…’, ‘7’, '5FV47683TK31403… in /home/polislatchiaptsv/public_html/main/data/RecordPayPalBooking.php on line 22

<?php
declare(strict_types=1);

ini_set("display_errors","1");
ERROR_REPORTING(E_ALL);
include_once('UniversalConnect.php');

class RecordPayPalBooking
{
  private $tableMaster;
  private $hookup;

  public $prices = array();            
  public $discounts = array();
  public $availability = array();
  public $sql4 = "";

  public function __construct($name,$email,$phone,$comments,$country,$price,$discount,$days,$mobile,$dateStart,$dateEnd,$People,$agree,$prop,$orderID,$paypal_name,$paypal_email,$paypal_amount)
  {

    $dateStart1 = date('Y-m-d', strtotime($dateStart));
    
    $dateEnd1   = date('Y-m-d', strtotime($dateEnd));  // https://www.sitepoint.com/community/t/debug-this-date-extract-script/347786/2
    
    // LEFT YYYY-MM-DD DATES
    $sql = "INSERT INTO `availability_paypal2`(`Property_ID`, `dateStart`, `dateEnd`, `paypal_id`, `paypal_name`, `paypal_email`, `paypal_amount`, `name`, `people`, `mobile`, `telephone`, `country`, `email`, `info`, `price`, `discount`, `days`, `agree`) 
    VALUES ($prop, '$dateStart1', '$dateEnd1', '$orderID', '$paypal_name', '$paypal_email', $paypal_amount, '$name', '$People', '$mobile', '$phone', '$country', '$email', '$comments', $price, $discount, $days, 1)";

    $this->sql4 = $sql;
    
  }
	
	
	public function exeSQLquery() { 
     
    $this->hookup=(new UniversalConnect)->doConnect();		
    
    
    try {
      
      $stmt4 = $this->hookup->prepare($this->sql4); 
      $stmt4->execute();
      return "true";
    }
    catch(PDOException $e) {
      return "Error: " . $e->getMessage();
    }			


    $this->hookup = null;
		
	}
	
}

I’m confused as to why you have those separate lines of code when the function you also posted seems to do the same thing.

That message suggests that the date strings are these:

‘02/14&#x2F…’, ‘02/27&#x2F…

which might explain part of the original issue. It’s encoding the second forward-slash for some reason, so your explode() or str_replace() doesn’t find it.

This is because you pass the invalid date into strtotime(), which returns false, and that is passed into date() where it’s not happy about it.

sorry in case I did Not use declare(strict_types=1);
getting
Error! true
AND YES I write in DB table SUCCESS BUT DATES
1970-01-01
1970-01-01
is it because I have the MySQL Date field? what to do in MySQL or PHP to get 2020-02-13 IN MYSQL TABLE FIELD…?

this seems problematic

$dateStart1 = date('Y-m-d', strtotime($dateStart));

when accepts: // gives 1970-01-01

02/13/2020 12:00 AM

server i think is in Europe time

Try this:

echo '<pre>';
  if(01):
    date_default_timezone_set('Europe/London');
    echo '<br><br><br><b>'. date_default_timezone_get() .'</b>';
    echo '<br>$dateStart4 ==> ' .$dateStart4  = '13-02-2020 04:00 AM';
    echo '<br>$dateEnd4 ==> '   .$dateEnd4    = '20-02-2020 04:00 AM';

    echo '<br><br><b>Formatted dates: </b>';
    echo '<br>$date ==> ' .$dateStart4 .' ==> ' .date('Y-m-d', strtotime($dateStart4));
    echo '<br>$date ==> ' .$dateEnd4   .' ==> ' .date('Y-m-d', strtotime($dateEnd4));
  endif;  

  if(1) :  
    date_default_timezone_set('Asia/Krasnoyarsk');
    echo '<br><br><br><b>'. date_default_timezone_get() .'</b>';
    echo '<br>$dateStart4 ==> ' .$dateStart4  = '02/13/2020 04:00 AM';
    echo '<br>$dateEnd4 ==> '   .$dateEnd4    = '02/20/2020 04:00 AM';
    
    echo '<br><br><b>Formatted dates: </b>';
    echo '<br>$date ==> ' .$dateStart4 .' ==> ' .date('Y-m-d', strtotime($dateStart4));
    echo '<br>$date ==> ' .$dateEnd4   .' ==> ' .date('Y-m-d', strtotime($dateEnd4));
  endif;  

  if(1) :  
    date_default_timezone_set('America/Chicago');
    echo '<br><br><br><b>'. date_default_timezone_get() .'</b>';
    echo '<br>$dateStart4 ==> ' .$dateStart4  = '02/13/2020 04:00 AM';
    echo '<br>$dateEnd4 ==> '   .$dateEnd4    = '02/20/2020 04:00 AM';
    
    echo '<br><br><b>Formatted dates: </b>';
    echo '<br>$date ==> ' .$dateStart4 .' ==> ' .date('Y-m-d', strtotime($dateStart4));
    echo '<br>$date ==> ' .$dateEnd4   .' ==> ' .date('Y-m-d', strtotime($dateEnd4));
  endif;  

  echo '</pre>';
  echo '<br><br>';

Output:

Is it really time zone problem?

this seems problematic

$dateStart1 = date(‘Y-m-d’, strtotime($dateStart)); // in other php file calling function from echo this is 02/13/2020 12:00 AM // also note date come from jQ UI datetime field as 02/13/2020 12:00 AM as here http://www.mkgeneralaid.com/propertyDetails.php?ID=7 // Book Here tab

when accepts: // gives 1970-01-01 in db table

$dateStart1 = date(‘Y-m-d’, strtotime(‘02/13/2020 12:00 AM’));

when accepts: // gives 2020-02-13 in db table

where you think the problem is???

I am confused and think you should start again by describing available resources, dates, time zones and the desired formatted result.

In PHP $dateStart1 = date(‘Y-m-d’, strtotime($dateStart))

$dateStart this what type must be? string or date object in PHP7…??? how convert date obj to string obj in php7???

Try this:

$dateStart1 = date(‘Y-m-d’, strtotime($dateStart))

echo gettype( $dateStart1 );
die;

https://www.php.net/manual/en/function.gettype.php

Edit:

var_dump($dateStart1); shows the type and the value.

1 Like

Error! string(29) “02/19/2020 12:00 AM” string(29) “02/28/2020 12:01 AM” !
returned by:

var_dump($dateStart4);
var_dump($dateEnd4);
die();

I think __construct must add this:

return $this;
} // so use this>>

$data = new RecordPayPalBooking($name,$email,$phone,$comments,$country,$price,$discount,$days,$mobile,$dateStart4,$dateEnd4,$People,$agree,$propNo,$orderID,$paypal_name,$paypal_email,$paypal_amount);
$tr1 = $data->exeSQLquery();

SAME PROBLEM
1970-01-01 IN DB TABLE DATE FIELD
string(29) “02/19/2020 12:00 AM” string(29) “02/28/2020 12:01 AM”

here >>

$dateStart1 = date('Y-m-d', strtotime($dateStart));

$dateEnd1   = date('Y-m-d', strtotime($dateEnd)); 

getting 1970-01-01
any other function try apart from
$dateEnd1 = date(‘Y-m-d’, strtotime($dateEnd));

??