I'm trying to insert a date value in a MySQL table like this (using Laravel 5.1):
public function update(Request $request, $id)
{
$user = User::findOrFail($id);
$data = $request->all();
$data['birthdate'] = date("Y-d-m", strtotime($data['birthdate']));
...
$user->fill($data)->save();
It works fine if I insert a date which the day < 12, while it'll be inserted 1970-01-01 if day > 12 !
I've used Eloquent mutators in User model like this :
public function getBirthdateAttribute() {
return date('d/m/Y', strtotime($this->attributes['birthdate']));
}
public function setBirthdateAttribute($value) {
$this->attributes['birthdate'] = Carbon::createFromFormat('Y-m-d', $value);
}
Please what's the matter with my code !?
change this:
date("Y-d-m", strtotime($data['birthdate']));
to:
date("Y-m-d", strtotime($data['birthdate']));
http://dev.mysql.com/doc/refman/5.7/en/date-and-time-literals.html
where your should be like this format dd-mm-yyyy, and if it like this dd/mm/yyyy
$date = '22/05/2012';
$date = str_replace('/', '-', $date);
$date = date('Y-m-d', strtotime($date));
This is the piece of the strtotime() manual you need to read and understand
Note:
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. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
So because you are using the / as a seperator strtotime() is assuming the American date format and getting the day and month parts confused
Related
Im submitting a form that has a date range picker in it.
I have the code to get the value of the picker from the form, which is in this format:
01/03/2017 - 01/03/2017
I also have it so that it splits the date range into two variables.
$one = 20/01/2017
$two = 13/03/2017
Im now trying to format the date of these variables into the one that MYSQL uses.
My problem is that the end date will fail and be displayed as 1970-01-01 all the time.
// Get the range from the form
$daterange = $_POST["daterange"];
// Split the range into a start and end date
list($one, $two) = explode("-", "$daterange", 2);
// Test the split worked
echo $one;
echo $two;
// Format the start date into MySQL style
$StartDate = date("Y-m-d", strtotime($one));
// Test date
echo $StartDate;
// Format the end date into MySQL style
$EndDate = date("Y-m-d", strtotime($two));
//Test date
echo $EndDate;
Personnaly, I prefer the date_create_from_format that does not presume anything.
One-liner :
$StartDate = date_create_from_format( "d/m/Y" , $one )->format("Y-m-d");
Try this:
$one = '20/01/2017';
$one = str_replace('/', '-', $one);
echo date('Y-m-d', strtotime($one));
// Output: 2017-01-20
Working Code
For the reason, see The PHP Manual for strtotime() Specifically this note
Note:
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. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
Use this to format your date:
$StartDate = date_format(date_create_from_format('d/m/Y', $one), 'Y-m-d');
To reverse:
$reverse = date('d/m/Y', strtotime($StartDate));
Using the following code to try and get 'Y-m-d', and should return 2016-05-10, but it is instead returning 2016-10-05.
// m-d-Y (Month-Day-Year)
$test_date = '05-10-2016';
// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));
echo $convert_date;
How do I get 'Y-m-d' returned? Not trying to use explode('-', $test_date). Is this possible to do using proper time functions?
Yes, use DateTime object for this:
$test_date = '05-10-2016';
$DateTime = DateTime::createFromFormat('m-d-Y', $test_date, new DateTimeZone('utc'));
var_dump($DateTime);
OUTPUT
object(DateTime)[8]
public 'date' => string '2016-05-10 15:08:53.000000' (length=26)
public 'timezone_type' => int 2
public 'timezone' => string 'UTC' (length=3)
So
echo $DateTime->format('Y-m-d'); //2016-05-10
Note: Be aware of dates in the m/d/y or d-m-y formats; if the separator is a slash (/), then the American m/d/y is assumed. If the separator is a dash (-) or a dot (.), then the European d-m-y format is assumed. To avoid potential errors, you should YYYY-MM-DD dates or date_create_from_format() when possible.
Source: w3schools
You have to convert '-' by '/'.
<?php// m-d-Y (Month-Day-Year)
$test_date = str_replace('-', '/', '05-10-2016');
// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));
echo $convert_date; // 2016-05-10
strtotime assume European formatted dates if they are seperated by - and USA date format if they are seperated by /
Note: from the manual 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. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
So you can just str_replace the - for /
// m-d-Y (Month-Day-Year)
$test_date = '05-10-2016';
$test_date = str_replace('-', '/', $test_date);
// Convert to Y-m-d
$convert_date = date('Y-m-d', strtotime($test_date));
echo $convert_date;
Or better still use the DateTime object
I find a problem related to the date function in php
i want to convert a date '04-08-2016'(mm-dd-yyyy) into a different format '2016-04-08'(yyyy-mm-dd). But it produces the result as '2016-08-04'(yyyy-dd-mm) instead of '2016-04-08'(yyyy-mm-dd).
my code is
$date = '04-08-2016';
echo date('Y-m-d',strtotime($date));
If i place '/' in place of '-' then it is working fine.
Can anyone tell me why this is happening?
You can use the DateTime object:
$date = '04-08-2016';
$d = DateTime::createFromFormat("m-d-Y", $date);
echo $d->format("Y-m-d");
The reason you need to do this is date conventions.
As specified in http://php.net/manual/en/function.strtotime.php
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. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
Try this:
$date = '04-08-2016';
$timeArray = strptime($date, '%m-%d-%Y');
$timestamp = mktime(0, 0, 0, $timeArray['tm_mon']+1, $timeArray['tm_mday'], $timeArray['tm_year']+1900);
echo date('Y-m-d', $timestamp);
This allows you to specify the format yourself.
I am writing below code to convert the data to Date.
$date = strtotime($request->input('DueDate'));
$Job->DueDate = date('Y-m-d', $date);
$Job->save();
But this saves the data as 0000-00-00
Try this maybe: (createFromFormat's first argument should be the format which is being received by the user)
$dt = \DateTime::createFromFormat('m/d/Y', strtotime($request->input('DueDate')));
$Job->DueDate = $dt->format('Y-m-d');
$Job->save();
Cheers,,
Let say your DueDate is correctly return the data. Try this:
$date = Carbon::parse($request->input('DueDate'))->format('Y-m-d');
$Job->DueDate = $date;
$Job->save();
Use carbon instance. For more docs Carbon
From the PHP manual 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. If, however, the year is given in a two digit format and the separator is a dash (-, the date string is parsed as y-m-d.
To avoid potential ambiguity, it's best to use ISO 8601 (YYYY-MM-DD) dates or DateTime::createFromFormat() when possible.
So the simplest mod for your situation would be to just convert the slashes to hyphens like this
$fixDate = str_replace('/', '-', $request->input('DueDate'));
$date = strtotime($fixDate);
$Job->DueDate = date('Y-m-d', $date);
$Job->save();
I have a problem with strtotime function in php, i'm trying to convert mm-dd-yyyy to yyyy-mm-dd. The date is entered on the text box and on submit it should save the date to the database. the problem is it returns a wrong date(1970-01-01) each time,which means my code isn't taking the variable i use to store the date, My code is:
//dateconvert
$submitdate = date($_POST['date']);
$date = date("Y-m-d", strtotime($submitdate));
//storeindb
$query ="INSERT INTO ticket SET date = '$date'";
$result = mysql_query($query);
I'm a newbie,please help.
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.
http://php.net/manual/en/function.strtotime.php
You need to use the forward slash separator for mm/dd/yyyy formats.
Use the code as:
$submitdate = $_POST['date'];
$date = date("Y-m-d", strtotime($submitdate));
Hope this hepls.
date format in $submitdate is incorrect Because the format yyyy/mm/dd should be yyyy-mm-dd so you will need to replace your / characters.
Try this:
$submitdate = str_replace("/","-",$_POST['date']);
echo date('Y-m-d', strtotime($submitdate));