I am trying to convert a date using pickadate.js mixed with PHP using Laravel 4. The issue I am having is when I attempt to save dates that are > 2013. If I choose Jan 23th 2014 it will save as Jan 23th 2013.

This is what's being sent via $_POST to the date variable.

23 January, 2014

This is my setup

$date = strtotime($scheduler['date']);

Converts to UNIX 1358993640 (which reads Jan 23 2013)

$dateFormat = date('l: F d, Y',$date);

Which becomes:

Wednesday: January 23, 2013 

Is there another function I could use? Or do I need to convert the time another way before strtotime? It works as long as it's 2013. So I am thinking once it hits 2014 it will work then also.

有帮助吗?

解决方案 2

Very strange. But found a fix. Testing on my local setup running PHP 5.5, it seems that the comma is what is causing the issue. So stripping out commas from the entered data produces the desired results with your test code:

// Set the test data.
$test_data = '23 January, 2014';

// Filter out commas from the '$test_data'
$test_data = preg_replace('/,/', '', $test_data);

// Get the Unix datetime from the test data.
$date = strtotime($test_data);

// Format the Unix datetime.
$dateFormat = date('l: F d, Y', $date);  

// Output for debugging.
echo 'date: ' . $date . '<br />';
echo 'dateFormat: ' . $dateFormat . '<br />';

The output I get is:

date: 1390453200
dateFormat: Thursday: January 23, 2014

其他提示

Assuming the textual date was created according to a fixed format, you can use DateTime::createFromFormat instead:

$date = DateTime::createFromFormat('d F, Y', $scheduler['date']);
echo $date->format('l: F d, Y');

Alternatively, just omit the comma:

echo date('l: F d, Y', strtotime('23 January 2014'));
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top