Convert date to M-d-y format in PHP [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
How do I change the date format using PHP? I have used the following code but it is displaying the date input field value as m-d-Y. Why it is changing the format?
$effectivedate = date('M-d-Y', strtotime($empCompensationdata-effective_date));
$effective_date = date('Y-m-d', strtotime($empCompensationdata-effective_date));
$("#effective_date").val($effectivedate);
$('#effective_date').attr('data-value', $effective_date);

The reason the value is displayed as M-d-Y is because that's what you're assigning to it with the val() method:
$effectivedate = date('M-d-Y', strtotime($empCompensationdata->effective_date));
$effective_date = date('Y-m-d', strtotime($empCompensationdata->effective_date));
$("#effective_date").val($effectivedate); //This variable contains "M-d-Y"
$('#effective_date').attr('data-value', $effective_date);
So if you want to have Y-m-d, just change the assigning variable:
$("#effective_date").val($effective_date); //This variable contains "Y-m-d"

Related

PHP how to convert dd/mm/yy to yyyy-mm-dd [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 2 years ago.
I'm trying to convert convert dd/mm/yy to yyyy-mm-dd. After i researched several methods and I found this.
$var = '20/01/2021';
$date = str_replace('/', '-', $var);
$show_date = date('Y-m-d', strtotime($date));
It works fine with result 2021-01-20.
However, my input was two digit yy only which is 20/01/21 and the result became 2020-01-21.
With an ambigous date like you have it is better to use the DateTime class as you can set the input format. See the following example from the documentation:
$var = '20/01/21';
$date = DateTime::createFromFormat('d/m/y', $var); // "d/m/y" corresponds to the input format
echo $date->format('Y-m-d'); //outputs 2021-01-20

PHP: convert date time variable into date [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I'm trying to convert a date and time variable into date only in a different format:
$test_date = '01/07/2018 10:00-12:00';
$result = substr($test_date, 0, 10);
$newDate = date("Ymd", strtotime($result));
echo $newDate;
Expected result= 20180701
Actual result= 20180107
Tried all the relative variations I can find on here but keep getting 'Ydm' instead of 'Ymd'.
I'm I missing something obvious here?
Looks like it's being parsed 'wrongly' because it's not explicitly stated what the source format is, using DateTime objects, you can do (not tested):
$result = substr($test_date, 0, 10);
$dateTime = DateTime::createFromFormat('d/m/Y', $result);
$newDate = $dateTime->format('Y-m-d');
echo $newDate;

PHP separate variable container [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I have a variable that contains a date
$date = "04/18/2017 04:02 PM";
This comes from a date field, This field will always contain a date like this, and What I'm trying to do is separating the date from the time
So how do i go about getting only 04/18/2017 from the variable or the time 04:02 PM ?
Thanks.
Just do a simple explode:
$explode = explode($date, ' ', 2);
$date = $explode[0];
$time = $explode[1];

How to convert string "290416" in date format in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I need to convert string value "290416" which is actually date but not in correct format. I need to change it in date format like 29/04/16.
please help.
If you don't need it as a date but only in date format. Meaning you are not performing any date-functions on it but just displaying it as a date you could use
$str = '290416';
$arr = str_split($str, 2);
$date_string = $implode('/', $arr);
The most robust way will be to use createFromFormat, passing in your format and the string, and they you have a DateTime object and can do many things with it.
define('MY_DATE_INPUT_FORMAT', 'mdy');
define('MY_DATE_OUTPUT_FORMAT', 'm/d/y');
$inputDateString = '042916';
$dateObj = DateTime::createFromFormat(MY_DATE_INPUT_FORMAT, $inputDateString);
$outputString = $dateObj->format(MY_DATE_OUTPUT_FORMAT);
This can also be done procedurally:
$date = date_create_from_format(MY_DATE_INPUT_FORMAT, $inputDateString);
echo date_format($date, MY_DATE_OUTPUT_FORMAT);

PHP Date String Format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a string mm-dd-yyyy which i get from a form, which i want to store it in the database of the data-type DATE (yyyy-mm-dd).
How do i format the string and save it in the database ?
$new_format = date("Y-m-d", strtotime('04-28-2012'));
or
$date = new DateTime('04-28-2012');
$new_format = $date->format('Y-m-d');
or in PHP 5.5+
$new_format = (new DateTime('04-28-2012'))->format('Y-m-d');
Try
$date = DateTime::createFromFormat("m-d-Y", '02-15-2012');
echo $date->format('Y-m-d H:i:s') , "\n";
Output
2012-02-15 23:54:52

Categories