HTML5/Angularjs dates with PHP
June 16, 2021 3:42 AM   Subscribe

I have an html5 date field in an angularjs application which I'm trying to send to a php script to save but what the field is coming back with is not something I can use. eg. Thu Jul 01 2021 00:00:00 GMT+0100 (British Summer Time)

I've used angularjs and date fields before and just gotten the YYYY-mm-dd which is what I'm wanting and I'm not sure why this one is coming up so different. The field is very simple

input type="date" ng-model="dash.newTrial.date_samples_due" />

I tried putting it through strtotime but it returns false, so it can't parse what I'm getting.
posted by missmagenta to Computers & Internet (4 answers total)
This Stack Overflow question is similar.

As a workaround, on the PHP side the format string to parse the JS date time format is "D M d Y H:i:s e+".
posted by jedicus at 5:44 AM on June 16

I think it's probably the "(British Summer Time)" that is making strtotime() return false. If your example date string represents a consistent format for the input, you should be able to just strip that bit in parens off and get back your timestamp. Several ways you could get the substring. I would probably do something like:
$in_str = 'Thu Jul 01 2021 00:00:00 GMT+0100 (British Summer Time)';
$time_in_seconds = strtotime( array_shift(explode('(', $in_str)) );

posted by Press Butt.on to Check at 11:39 AM on June 16

The format of the date returned by Javascript's Date.toString() method is standardized and appears to be what is being used here, as an FYI. I'm not familiar with Angular specifically, but if there's a mechanism for getting at the underlying Date object before you send it off to the server, you can send a more readily machine-parseable format. Do be aware that it converts to UTC time if you use Date.toISOString(), and you'll need to account for that in your code.
posted by Aleyn at 6:19 PM on June 16

The Carbon extension for PHP makes working with dates easier and might be able to help.
posted by kirkaracha at 2:04 PM on June 17

« Older Kitchen organization... help   |   Do most other women get physical pleasure from... Newer »

You are not logged in, either login or create an account to post comments