Asked  2 Years ago    Answers:  5   Viewed   125 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

2

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, August 24, 2022
1

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'));
Saturday, December 24, 2022
2
  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 
Monday, November 14, 2022
 
2

strtotime is converting a string into a timestamp and date is converting a timestamp into a string, you need to reverse date with strtotime like so:

public function store(ScheduleRequest $request)
{
    $schedule = new Schedules;
    $schedule->allDay = $request->allDay;
    $schedule->start = date('Y-m-d H:i:s', strtotime($request->start));
    $schedule->end = date('Y-m-d H:i:s', strtotime($request->end));
    $schedule->title = $request->title;

    if ($schedule->save())
    {
        return [
            'success' => 'Data Was Saved Successfully'
        ];
    }
}
Saturday, October 8, 2022
1

Because you are passing a string as the second argument to the date function, which should be an integer.

string date ( string $format [, int $timestamp = time() ] )

Try strtotime which will Parse about any English textual datetime description into a Unix timestamp (integer):

date("d", strtotime($_GET['start_date']));
Monday, November 28, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 

Browse Other Code Languages