Date format not working correctly - php

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));

Related

Adding x no of days to given date, but it is taking the day number as month and doing the calc. Help me fix the format

I m trying to add 28 days to the given date and echo, however it is taking the given date and considering it as month and adding the days given
Help me fix the format.
my code goes as:
$start = "04/03/2019";
$nextpay1 = date('d/m/Y',strtotime($start . "+28 days"));
echo $nextpay1;
as result it is echoing "01/05/2019", which is not right..
I'll start my answer by quoting the strtotime docs:
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, in your case:
$date = DateTime::createFromFormat('d/m/Y', '04/03/2019');
$date->modify('+28 days');
echo $date->format('d/m/Y');
The problem is that $start is being parsed in the default mm/dd/yyyy format. You should use a function to parse it as the format you intend, and then add to that.
$parsed = date_parse_from_format('d/m/Y', $start);
$startdate = mktime(
$parsed['hour'],
$parsed['minute'],
$parsed['second'],
$parsed['month'],
$parsed['day'],
$parsed['year']
);
$nextpay1 = date('d/m/Y', strtotime('+28 days', $startdate));
$date=Date('y:m:d', strtotime("+3 days"));
This will save the date after 3 days

time() gives the same value for two different dates

I have the following codes here:
$start_date = date_parse('10-17-2017');
$end_date = date_parse('10-30-2017');
$start_time = time($start_date);
$now = time();
$end_time = time($end_date);
var_dump($start_time,$end_time, $now);
The output is this:
int(1508383949) int(1508383949) int(1508383949)
Why are they giving the same values?
I also tried using strtotime,
$start_time = strtotime($start_date);
$now = time();
$end_time = strtotime($end_date);
And had this as the result:
bool(false) bool(false) int(1508384298)
The time function does not take any arguments. So the following wont work.
time($start_date);
with strtotime you need to have your date in either m/d/y or d-m-y format. Please check the hyphen and slash.
From the official docs of strtotime
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.
Top of the mornin’ to you,
You get the same values, because
time doesn't accept any parameters
you use date_parse / strtotime not correctly
strtotime allows - next to many others - this 2 formats for dates: m/d/y and d-m-y. So you need to change your values from 10-17-2017 to 10/17/2017 or to 2017-10-17.
My Solution:
<?php
$start_time = strtotime('10/17/2017');
$end_time = strtotime('10/30/2017');
$now = time();
var_dump($start_time, $end_time, $now);
Output:
int(1508212800) int(1509336000) int(1508385505)
You find more informations on PHP.net, just look for the strtotime and the time functions. :3
Sincerely,Sam

PHP date function not working at certain condition

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.

Saving incorrect date in MySQL Database

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();

How to convert date to different format

I have a biometric device which gives date format in d/m/Y h:i:s but I want to convert the same information to Y-m-d.
$var = d/m/Y h:i:s ;
echo convert($var); // Output want: Y-m-d
But if date is 10-11-2015 then how php will know that the format is d-m-y or m-d-y ?
I know how to convert yyyy-mm-dd to dd-mm-yyyy, but want dd-mm-yyyy to yyyy-mm-dd
How Can I do that ?
use the DateTime class
$date = DateTime::createFromFormat('d/m/y', '28/11/15');
echo $date->format('Y-m-d');
outputs
2015-11-28
Or you could do it on one line if you like that sort of thing:
echo \DateTime::createFromFormat('d/m/y', '28/11/15')->format('Y-m-d');
also read the note from here manual
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.
Try this:
$var = date('d/m/Y h:i:s');
$datetime = explode(' ', $var);
$date_string = implode('-', array_reverse(explode('/', $datetime[0])));
$time_string = $datetime[1];
$formated = date('Y-m-d', strtotime("{$date_string} {$time_string}"));
echo "<pre>";
print_r($formated); // test output
echo "</pre>";

Categories