Asked  7 Months ago    Answers:  5   Viewed   38 times

I unfortunately can't use DateTime() as the server this project is on is running PHP v.5.2.

the line in question:

$aptnDate2 = date('Y-m-d', $_POST['nextAppointmentDate']); 

throws the following error:

Notice: A non well formed numeric value encountered

so I var dump to make sure it's well formatted..

var_dump($_POST['nextAppointmentDate']);  

string(10) "12-16-2013"

The php docs state that it takes a timestamp not a string. but when I do:

date('Y-m-d', strtotime($_POST['nextAppointmentDate']));

and then var_dump the result, I get this:

string(10) "1969-12-31"

why can I not format a date with this date value and strtotime()?

thanks!

 Answers

83

From the documentation for strtotime():

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

In your date string, you have 12-16-2013. 16 isn't a valid month, and hence strtotime() returns false.

Since you can't use DateTime class, you could manually replace the - with / using str_replace() to convert the date string into a format that strtotime() understands:

$date = '2-16-2013';
echo date('Y-m-d', strtotime(str_replace('-','/', $date))); // => 2013-02-16
Wednesday, March 31, 2021
 
ariel
answered 7 Months ago
28

How can I make it return today's date in that case?

pseudocode:

if (today == monday)
    return today;
else
    return strtotime(...);

Btw, this trick also could work:

strtotime('last monday', strtotime('tomorrow'));
Wednesday, March 31, 2021
 
Terry
answered 7 Months ago
89
  1. The reason for the error is that there is nothing like Mon, 21 May 2013 from my calendar 21st May is Tuesday

  2. From PHP DOC

Dates in the m/d/y or d-m-y formats are disambiguated by looking at the separator between the various components: if the separator is a slash (/), then the American m/d/y is assumed; whereas if the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed.

To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.

Examples

$strtotime = strtotime("Tue, 21 May 2013 09:10:30 GMT");
echo date("Y-m-d H:i:s", $strtotime),PHP_EOL;

$strtotime = DateTime::createFromFormat("D, d M Y g:i:s O", "Tue, 21 May 2013 09:10:30 GMT");
echo $strtotime->format("Y-m-d H:i:s");

Output

2013-05-21 11:10:30  <- strtotime
2013-05-21 09:10:30  <- datetime 
Wednesday, March 31, 2021
 
Owen
answered 7 Months ago
87

This means that the second parameter for date() is expecting integer, so convert $row['posted'] to timestamp first.

Try

$posted = date('d/m/Y H:i:s', strtotime($row['posted']));
Monday, August 16, 2021
 
ajaybc
answered 2 Months ago
80

$offset is supposed to be numerical, but you are assigning a string to it in the while itself:

echo $offset = $stringpos + $search_length.'<br/>';

After this line, $offset is no longer a number and the second time the while is executed, you'd get that notice

Tuesday, August 31, 2021
 
Silfverstrom
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :