I have gone through convert php date to mysql format but still I have few issues and questions,
In general, my application, can input dates in Y-m-d, d-m-y, m-d-Y, jS, F Y format, or even with '/' separator.
I want this dates to be converted in to MYSQL Y-m-d format,
I tried as below, but it shows different output than expected,
Non working
date_format(date_create_from_format('d-m-Y', '02-25-2016'), 'Y-m-d');
Working
date_format(date_create_from_format('d-m-Y', '25-02-2016'), 'Y-m-d');
So it seems format and string to be match in same way, otherwise its not interpreting correctly,
What is best way to convert above 4 format inputs to mysql(Y-m-d) format?
Thanks advanced,
I'd recommend using Carbon for any date processing in PHP these days.
Creating date by parsing strings is simple enough...
http://carbon.nesbot.com/docs/#api-instantiation
Getting the output in a format you need is also simple...
http://carbon.nesbot.com/docs/#api-formatting
I'd suggest you to use this function: DateTime::createFromFormat
Using this, you need to create a date by specifying the particular format.
Try this:
$date = DateTime::createFromFormat('Y-m-d', '2016-05-26');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('d-m-Y', '26-05-2016');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('m-d-Y', '05-26-2016');
echo $date->format('Y-m-d');
echo "<hr/>";
$date = DateTime::createFromFormat('jS F Y', '26th May 2016');
echo $date->format('Y-m-d');
Thanks for all suggestions, i think i found my logic,
My date format selection is stored in sql db, so will try as below,
date_format(date_create_from_format('format from db', 'user entered format'), 'Y-m-d');
As user entered format is stored in db, so i will pull that in this date_format function, so both will match and i can convert to Y-m-d!
Thanks,
Related
I tried to use strtotime function to format the today's date in PHP but its giving me the wrong result. My code is given below.
<?php
$today = date("m-d-Y H:i:s");
echo date('m-d-Y H:i:s', strtotime($today));
?>
Here, I am getting this 01-01-1970 05:30:00 result.
Here, I need to get the proper datetime result.
date("m-d-Y") is what's causing issues for you. For example, take 01-02-2019 and 02-01-2019 - which is Februrary 1st and which is January 2nd? That format will make strtotime() return false, as it doesn't know what format that is for days that are greater than 12.
d-m-Y would be expected and a valid format.
You can use DateTime::createFromFormat() instead. Then you can create a valid DateTime object from that format, and use it however you need it to.
$today = DateTime::createFromFormat("m-d-Y H:i:s", date("m-d-Y H:i:s"));
echo $today->format("m-d-Y H:i:s");
Live demo
Documentation for DateTime::createFromFormat()
Alternatively, if you just need to print the date directly and not process it further, you don't need to go through any hoops and can just use date() as you were, without the second line. But you can not use that result in a strtotime() function, as it will return incorrect results.
echo date("m-d-Y H:i:s");
i'm compiling a church register and the dates are in this format yyyy-mm-dd
example. 1978-03-27 .I need to make it 27th March,1978. Any php script to help me out?
I tried MSEXcel but messes it up. thanks (anyway..i'm getting it from a database) so i would be glad if i have it been read from a database or a file of dates.
Try with:
$input = '1978-03-27';
$timestamp = strtotime($input);
$output = date('dS F, Y', $timestamp);
Reference to date format: php.net
Try this:
$in = '1978-03-27';
$out = date('jS F Y',strtotime($in));
var_dump($out);
date() formats your output, and strtotime() converts your input to a timestamp. Keep in mind this will only work for values from 1970-01-01. If you want to work with "older" dates, you have to parse the input manually and generate the output you require.
I store the date and time in mysql as a date/time field which has this format: 2012-03-12 14:51:26, what i am trying to do is simply rearrange the DD/MM/YY to look like this.
When i use the following code, it just gives me a date wrong format warning.
echo date_format($date, 'Y-m-d H:i:s');
If you are just displaying the date you can supply a certain format in the SQL query
SELECT DATE_FORMAT("%d/%m/%Y", date_column) FROM table
If you convert the MySQL timestamp to a unix timestamp, then you can use the date() function to output it in whatever format you like:
$unixTimestamp = strtotime($mysqlDate);
echo date($dateFormat, $unixTimestamp);
See the date format strings here: http://php.net/manual/en/function.date.php
First, convert it to a Unix timestamp (which I find to be all around better than a date_time field for a lot of reasons), then use PHP's date function.
echo date('Y-m-d h:i:s', strtotime($date));
Simply do:
date("d/m/Y", strtotime($date));
And read about strtotime function.
this will work.
$date = date_create("2012-03-24 17:45:12");
echo date_format($date, 'Y-m-d H:i:s');
I am using an html form to get a user inputted date. The structure of the date inputted is: MM/DD/YYYY. I then need to increment the total days by 196 in PHP. Right now, the data is being posted to a php file called Calculate.php. I was looking into altering the data using date (m d Y); in php, but my friend said that probably wont work. Any ideas? Thank you for your time and have a great day! ^_^
Check out strtotime. This will convert your MM/DD/YYYY format in to a numeric value you can then work with.
Use strtotime again to manipulate the date to add the days to it.
Use strftime to re-format it for display.
e.g.
$d = '08/11/2011';
$dAsPOSIX = strtotime($d);
$dPlus196Days = strtotime('+196 day', $dAsPOSIX);
echo strftime('%m/%d/%Y',$dPlus196Days);
DEMO
Using strtotime magic:
strtotime('08/11/2011 +196 days');
$input = $_POST['date'];
echo date('m d Y', strtotime('+196 days', strtotime($input));
Duplicate
Managing date formats differences between PHP and MySQL
PHP/MySQL: Convert from YYYY-MM-DD to DD Month, YYYY?
Format DATETIME column using PHP after printing
date formatting in php
Dear All,
I have a PHP page where i wil be displaying some data from Mysql db.
I have 2 dates to display on this page.In my db table, Date 1 is in the format d/m/Y (ex: 11/11/2002) and Date 2 is in the format d-m-Y (ex : 11-11-2002)
I need to display both of this in the same format .The format i have stored in a variable $dateFormat='m/d/Y'
Can any one guide me
Thanks in advance
Use strtotime to convert the strings into a Unix timestamp, then use the date function to generate the correct output format.
Since you're using the UK date format "d/m/Y", and strtotime expects a US format, you need to convert it slighly differently:
$date1 = "28/04/2009";
$date2 = "28-04-2009";
function ukStrToTime($str) {
return strtotime(preg_replace("/^([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $str));
}
$date1 = date($dateFormat, ukStrToTime($date1));
$date2 = date($dateFormat, ukStrToTime($date2));
You should be all set with this:
echo date($dateFormat, strtotime($date1));
echo date($dateFormat, strtotime($date2));
You may want to look into the strptime function. This can convert any date from a string back into numeric values. Unlike strtotime, it can be adapted to different formats, including those from different locales, and its output is not a UNIX timestamp, so it's capable of parsing dates before 1970 and after 2037. It may be a little bit more work though because it returns an associative array though.
Unfortunately it's not available on Windows systems either so it's not portable.
If for some reason strtotime will not work for you, could always just replace the offending punctuation with str_replace.
function dateFormat($date) {
$newDate = str_replace(/, -, $date);
echo $newDate;
}
echo dateFormat($date1);
echo dateFormat($date2);
I know this will make most folks cringe, but it may help you with formatting non-date strings in the future.
rookie i am. so came up with the method that just do that. what mysql needs.. shish i used param 2... hope it helps. regards
public function dateConvert($date,$param){
if($param==1){
list($day,$month,$year)=split('[/.-]',$date);
$date="$year-$month-$day"; //changed this line
return $date;
}
if ($param == 2){ //output conversion
list($day,$month,$year) = split('[/.]', $date);
$date = "$year-$day-$month";
return $date;
}
}