Debug this date extract script

$dateEnd1 this entered in db sql query as

‘$dateEnd1’. date field,… well?

Error! string(29) “02/19/2020 12:00 AM” string(29) “02/28/2020 12:01 AM” string(10) “1970-01-01” string(10) “1970-01-01” !

FOUND ERRONEOUS PART THIS ^^^ CAME FROM. >>> date function problem…well ?

var_dump($dateStart);
var_dump($dateEnd);

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

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

var_dump($dateStart1);
var_dump($dateEnd1);

Try this before the error and gradually move down the script until the exact error is pinpointed.

Once found, display he line here and var_dump(…); all the relevant variable which are causing the error.

<?php 
...
...
...
echo '<br>' . __line__; die;
...
...

Edit:

Thanks to the Administrator for correcting my spelling mistake.

the error is ^^^ and is clearly shown…
before this vardump OK
after this vardump
1970-01-01

needed insert zone to work the
$y = date(‘Y-m-d’, strtotime($x));

???

Ok, immediately before the line var_dump(…); needs to be shown for $dateStart and $dateEnd.

Also if the values appear correct then try using the values as parameters in strtotime(…);

It is worth noting that the line could have been separated into two statements.

If they are not what is expected then step back through he script to see where the were given incorrect values.

I have a lot going on ATM which doesn’t help, but I’m confused and it looks like there’s more than one problem happening.

I easily recognize the “1970” problem, I’ve seen it plenty of times when I’ve worked with datetime in the past.

“unix epoch time” is - the number of seconds since Jan 1 1970 - so a zero would be that date.

That the value is in database fields suggests there is a problem with the code doing the INSERT or maybe the field datatype.

In any case I think it might be easier to put working with the query on a back burner for a while and get the PHP code working with some hard coded example values first.

1 Like

string(29) “02/19/2020 12:00 AM”
string(29) “02/28/2020 12:01 AM”
came from first vardump

FOUND ERRONEOUS PART THIS ^^^ CAME FROM. >>> date function problem…well ?

var_dump($dateStart);
var_dump($dateEnd);

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

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

var_dump($dateStart1);
var_dump($dateEnd1);

the latter vardump gives

string(10) “1970-01-01”
string(10) “1970-01-01”

but this
$dateStart2 = date(‘Y-m-d’, strtotime(‘02/19/2020 12:00 AM’));

$dateEnd2   = date('Y-m-d', strtotime('02/28/2020 12:01 AM')); \

var_dump($dateStart2);
var_dump($dateEnd2);

got this >>>

string(10) “2020-02-19”
string(10) “2020-02-28”

Please try the following so I can adjust my timezone and try the supplied variables.

echo '<br>'. date_default_timezone_get();

// also
echo '<br>' . $dateStart;
echo '<br>' .strtotime($dateStart); // should be integer with high value
echo '<br>' .$dateEnd;
echo '<br>' .strtotime($dateEnd); //another integer with high value

So it seems you have established that strtotime does not understand dates in the format you’re giving it. Instead of debugging the entire surrounding area of strtotime I’d suggest taking a completely different approach. Namely telling PHP exactly what the input format is, and tell it to parse the input using that format.

So basically, DateTime::createFromFormat(), which @m_hutley already suggested you use in post #3 of this thread :slightly_smiling_face:

https://www.php.net/manual/en/datetime.createfromformat.php

1 Like

I need help …how? this give error what to do???

echo date_default_timezone_get() . '<br>';

var_dump($dateStart);
var_dump($dateEnd);

$format = "d/m/Y h:i A";
$dateStart1 = DateTime::createFromFormat($format, $dateStart);
$dateEnd1 = DateTime::createFromFormat($format, $dateEnd);
$dateStart1 = $dateStart1->format('Y-m-d');  // line 44
$dateEnd1 = $dateEnd1->format('Y-m-d');

var_dump($dateStart1);
var_dump($dateEnd1);

echo "<br>";

result

Error! Europe/Athens
string(29) “02/13/2020 12:00 AM” string(29) “02/29/2020 12:01 AM”
Fatal error : Uncaught Error: Call to a member function format() on boolean in /home/polislatchiaptsv/public_html/main/data/RecordPayPalBooking.php:44 Stack trace: #0 /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php(192): RecordPayPalBooking->__construct(‘444444444444444…’, ‘info@paphos.cit…’, ‘444444444444444…’, ‘Notes\n---------…’, ‘Cyprus’, ‘2160’, ‘0’, ‘16’, ‘99323064’, ‘02/13&#x2F…’, ‘02/29&#x2F…’, ‘Neo Chorion’, ‘1’, ‘7’, ‘81073299SY07337…’, ‘Leonidas Savvid…’, ‘a080725@cytanet…’, ‘0.01’) #1 /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php(211): bookProcess(‘444444444444444…’, ‘info@paphos.cit…’, ‘444444444444444…’, ‘Notes\n---------…’, ‘Cyprus’, ‘2160’, ‘0’, ‘16’, ‘99323064’, ‘02/13&#x2F…’, ‘02/29&#x2F…’, ‘Neo Chorion’, ‘1’, ‘Irene Villa \xE2\x80\x94…’, ‘7’, ‘81073299SY07337…’, ‘Leonidas Savvid…’, ‘a080725@cytanet…’, ‘0.01’) #2 {main} thrown in /home/polislatchiaptsv/public_html/main/data/RecordPayPalBooking.php on line 44
!

when $format = “m/d/Y h:i A”; // again error

Error! Europe/Athens
string(29) “02/13/2020 12:00 AM” string(29) “02/29/2020 12:01 AM”
Fatal error : Uncaught Error: Call to a member function format() on boolean in /home/polislatchiaptsv/public_html/main/data/RecordPayPalBooking.php:44 Stack trace: #0 /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php(192): RecordPayPalBooking->__construct(‘444444444444444…’, ‘info@paphos.cit…’, ‘444444444444444…’, ‘Notes\n---------…’, ‘Cyprus’, ‘2160’, ‘0’, ‘16’, ‘99323064’, ‘02/13&#x2F…’, ‘02/29&#x2F…’, ‘Neo Chorion’, ‘1’, ‘7’, ‘8P9729475217175…’, ‘Leonidas Savvid…’, ‘a080725@cytanet…’, ‘0.01’) #1 /home/polislatchiaptsv/public_html/main/paypal-transaction-complete.php(211): bookProcess(‘444444444444444…’, ‘info@paphos.cit…’, ‘444444444444444…’, ‘Notes\n---------…’, ‘Cyprus’, ‘2160’, ‘0’, ‘16’, ‘99323064’, ‘02/13&#x2F…’, ‘02/29&#x2F…’, ‘Neo Chorion’, ‘1’, ‘Irene Villa \xE2\x80\x94…’, ‘7’, ‘8P9729475217175…’, ‘Leonidas Savvid…’, ‘a080725@cytanet…’, ‘0.01’) #2 {main} thrown in /home/polislatchiaptsv/public_html/main/data/RecordPayPalBooking.php on line 44
!

‘02/13&#x2F…’, ‘02/29&#x2F…’,

needed escape special chars???

2020-02-09_08-57-31

Ah! If I were to give you the date “2020-02-09”, what are the values for the the hour, minute and second for the date I gave you?

Try this as a standalone PHP file and change $dateStart and $dateEnd to match the dates in the main PHP file:

<?php 
declare(strict_types=1);
error_reporting( -1);
ini_set('display_errors', '1');

date_default_timezone_set('Europe/Athens');
echo '<br> default_timezone_set() ==> '. date_default_timezone_get();
echo '<br>';

echo '<br>Source dates:';
echo '<br> $dateStart ==> ',
  $dateStart = '02/13/2020';
echo '<br> $dateEnd ==> ',
  $dateEnd   = '02/29/2020';
 
echo '<br><br> $tStampStart  ==> ', 
  $tStampStart  = strtotime($dateStart); 
echo '<br> $tStampEnd ==> ', 
  $tStampEnd    = strtotime($dateEnd);  

echo '<br>';

  if($tStampStart && $tStampEnd) :
    echo '<br> $dateStart1 ==> ',
      $dateStart1 = date('Y-m-d', $tStampStart);
    echo '<br> $dateEnd1 ==> ',
    $dateEnd1   = date('Y-m-d', $tStampEnd);  
    
  else:  
    $style=' style="display:inline-block; background: #c00; color: snow"';
    echo '<h1 ' .$style .'>'
          .  'PROBLEM with dates: '
          .   '<br>'
          .   '$dateStart = ' .$dateStart
          .   '<br>'
          .   '$dateEnd = ' .$dateEnd
          .'<h1>'
          ;
    die('PLEASE FIX THE DATE PROBLEM');    
  endif;  

If you are satisfied that all incorrect dates are caught then remove all lines starting with echo and insert into the main file.

Good Output

Bad Output

Though if he’s got oddball characters in there instead of normal slashes, it’s not going to work either :stuck_out_tongue:
For some reason part of the date has been rendered into an HTML entity rather than proper string, at least thats what post 32 is suggesting.

When I was on the desktop I tried using the DateTime class and also got errors when calling the following script that produced errors in post #31

$dateStart1 = $dateStart1->format(‘Y-m-d’); // line 44

Using the timestamp from the strtotime(…) result only produced errors when there was an invalid $date string.

Also in post #31 the var_dump(…) result was:

string(29) “02/13/2020 12:00 AM

which is an acceptable strtotime(…) parameter.

I agree.

strtotime is certainly usable. It can occaisionally trip people up (What format is 04/08/2020 in? Am I talking about April or August?), so I do prefer the readability of specifying the input format when possible, but either is equally workable.

What is common style used about dateIn & dateOut dates for real estate rentals Websites?

I think this is the problem the Topic is trying to solve!

Everyone assumed the date was in the specified format and it appears the original input date was not validated.

There are numerous input date routines, the simplest requires three drop-down lists one each for the day, month and year.

My PHP7 CODE ALL WAS OK
PROBLEM WAS IN JS escapeHTML($(‘#pickupDate’).val()), REMOVED ESCAPE Function… all working ok now from first coding post here

                dateStart: $('#pickupDate').val(),
                dateEnd: $('#dropoffDate').val(),
                agree: escapeHTML($('input[name="agree"]:checked').val()),
                country: escapeHTML($('#country').val()),
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.