Date formatting - DD-MM-YYYY to YYYY-MM-DD - php

MySQL table has 2 columns - startdate and enddate
They hold dates in this format: 22-01-2016
When I was prototyping, I used MediumText.
Now, I want to switch to Date format. The default date format in PHPMyAdmin is YYYY-MM-DD
If I switch the columns to Date, my data turns into 0000-00-00.
Using PHP or MySQL, how do I reformat my data prior to going back into PHPMyAdmin to switch the columns to Date format?

You can use string to date function to update columns data before changing its structure
UPDATE table SET dateColumn = STR_TO_DATE(dateColumn, '%d-%m-%Y');
It will convert DD-MM-YYYY formatted dates to YYYY-MM-DD. After that you can just change structure of column to DATE

The easiest and most reliable way would be to split up the dates and rearrange them the other way round:
$parts = explode('-', $olddate);
$newdate = sprintf('%04d-%02d-%02d', $parts[2], $parts[1], $parts[0]);

Related

How to insert a datetime at european format in BDD

I insert a date in PHP created like that :
$myDate = date('Y-m-d H:i:s');
It works perfectly. If I change the format like that :
$myDate = date('d-m-Y H:i:s');
The value inserted is 0000-00-00...
It exsists a way to insert a date in French format ?
MYSQL only allows yyyy-mm-dd format for storage so it is impossible to store dates in the French format.
You can change the field type to be a char or any other string type if you want to store the dates in any format of your choice but you won't be able to perform MYSQL date functions, during queries, on the field.
It is better to follow the standard format and have a script convert the date to whatever format you want when you want to display the date to users.

Insert date in specific format into MySQL db

I need to insert the current date in the following format into a TIMESTAMP column in a MySQL db: d-m-Y
As of now I am using SQL NOW(), which returns the date as Y-m-d. Because I am using AJAX to display the data I cannot format the returned result using $date_returned->format(d-m-Y). Therefore I need to insert the date in the format that I will display on my AJAX call.
I tried to insert the date using the following functions:
1) date('d-m-Y');
2) (new \DateTime())->format('Y-m-d');
I understand these two functions do pretty much the same thing but I was not sure what else I should try.
MySQL threw the following error for both dates:
Error : (1292) Incorrect datetime value: '-2014' for column 'msg_date' at row 1
I am guessing this should be an easy fix but I can't figure out what is wrong.
I tried both TIMESTAMP and DATETIME on MySQL's end but neither worked. (I need it to be TIMESTAMP though).
Any suggestion is welcome!
$newdate= date('Y-m-d', strtotime('10-09-2015'));
or if you want current time just use
$now = date('Y-m-d');
If your msg_date column's structure is DATETIME or TIMESTAMP, the date format should be:
YYYY-MM-DD HH:MM:SS
which can be formatted through PHP like this:
$date = date("Y-m-d H:i:s");
Or if you already have a date, and you want it to convert to that format, we can use strtotime():
$date = date("Y-m-d H:i:s", strtotime($date));
For more date format, check this link.
The MySQL error message indicated that you had the date format the wrong way around.
Year must go first, then month, then day, as in:
date('Y-m-d') // right
In your first example, you have
date('d-m-Y') // wrong
In one of your examples above, you have it right, but you say you got the same response, so I assume that was not what you actually tried.
Another thing to note is that a MySQL TIMESTAMP column stores both a date and time. It's valid to give MySQL just a date (MySQL will just leave the time at zero), but if you have no need to store a time, you may as well make the column DATE instead of TIMESTAMP.
If you want to display your dates as d-m-Y then by all means do so, but they need to be sent to MySQL as Y-m-d.

Date to Timestamp Help required

I am using jQuery DatePicker to select from date and to date. when I select a date i covert it to timestamp using strtotime function, I have some dates stored in my database with column type timestamp.
when I convert the selected date from date picker using strtotime
$from_date = strtotime($form['from-date']);
it gives me something like this
1338508800
while the date stored in my database table looks like this
2012-06-09 02:24:25
I want to query the records on the basis of the selected from and to date. but the query is not getting my dates correctly.
How can I change the selected date so that it give me results in between the two selected dates?
You want date('Y-m-d H:i:s',strtotime($form['from-date']));

How to deal with DATE-type in MYSQL & PHP?

I'm building a website with php and i'm using the DATE-type in my MYSQL table to store dates. The problem that i have is that this stores the dates by default in the format YYYY-MM-DD. But i need this format DD-MM-YYYY to appear on my PHP page with the possibility of calculating the amount of days between 2 different dates. How can this be achieved?
That's a display problem. Use mysql's date_format() function to convert to whatever your display requirements are, e.g.
SELECT date_format(datefield, '%d-%m-%Y'), ...
You can use strtotime to convert a string representation of the date to an actual date object in php, then use the date function to spit out the date as any string format you wish. Also, you can be strtotime to perform date calculations. Additional information can be found at this blog post.
$phpDate = strtotime($stringDateFromDb);
date('d-m-y', $dateFromDb);
strtotime('-3 days', strtotime($stringDateFromDb));
Here is an example for a way to do it:
$date_str = '2012-05-20'; //you get it from db
$date_now = strtotime($date_str); //convert it to unix timestamp
$yesterday=$date_now-24*60*60; //make calculations
echo 'yesterday was: '. date('d-m-Y',$yesterday); //date() returns the date in format you need
Further example here: How to calculate the difference between two dates using PHP?

PHP MySQL date functions

I am working with dates, in both PHP as well as MySQL. EVerytime I use to convert date in unix format. But this time I have taken field in DB as date. But issue is it is taking yyyy-mm-dd format. I want to store it in dd-mm-yyyy format. Is this possible if I set default setting of DB. or each time I have to explode the dd-mm-yyyy format in PHP and convert it in YYYY-MM-DD format. Its my first query.
Second query is I wish to fetch the records from today's date. I mean dates after today's date. Like today then tomorrow then so on.... Is it possible to use order by on date field.
Just use:
$date = date('d-m-Y', strtotime($dateFromDB));
That will convert from MySQL DateTime to the format you have specified.
It is possible to order by date fields, e.g.:
SELECT *
FROM table
WHERE date > [yourDate]
ORDER BY date [DESC | ASC]
Your second requirement contradicts with the first one.
If you store your date in dd-mm-yyyy format, you'll be unable to sort your dates.
So - yes, you have to "explode" the dd-mm-yyyy date in PHP or format it any other way. That's not a big deal though. Everyone does it.
If you have a field of type 'datetime' you can use the MySQL-Command: FROM_UNIXTIME(%d) for conversion. 'order by' should be no problem.
Store the date in default format that is yyyy-mm-dd
when you want to display in front end
use the following query to
select otherFields, date_format(dateField,'%d-%m-%Y') from tableName;
For ordering by date
SELECT * FROM tbl
ORDER BY date DESC

Categories