Converting date to country requirements - php

I have a calendar in html form and I want to insert this date into MySQL. The default MySQL date is 0000-00-00. But in my country the format is DD/MM/YYYY. So what to do to fix it. Thank you. I am using PHP.

You must use one format in your HTML page, and another format in your database.
So, if you want to store a date like this '12/05/2008' into mySql, you must transform it like this:
$date = '12/05/2008';
$dateToStore = date('Y-m-d', strtotime(str_replace('/','-',$date)));
And if you wonder why, you need to replace the '/' with '-' to make php know that the first part of the data string is the day, and then the month (as I think is your case).

MySQL the date format is always YYYY-MM-DD. To convert it to another format, you need to manually convert the retrieved date to the desired format like
$displayDate=date("d/M/Y", strtotime($mysqldate));

Method 1
You cant insert into DD/MM/YYYY format. Instead while rendering it in view file you can change into desired format.
<?php
$date = $result['db_date']; // I ASSUMED YOUR DB FIELD IS db_date
$desiredFormat = date('d/m/Y', strtotime($date)); // CONVERTING INTO YOUR FORMAT
echo '<pre>'; print_r($desiredFormat); // DISPLAYING IT
?>
Method 2
You can retrieve from database in your desired format using below
SELECT *, DATE_FORMAT(YOUR_DATE_FIELD, "%m/%d/%Y") AS date FROM YOUR_TABLE;

Use MySQL STR_TO_DATE
Try this mysql query :-
INSERT INTO `table`(`date`) VALUES (STR_TO_DATE('10/10/2015', '%d/%m/%Y'))

Related

I need help to sort out my incorrect query that doesn't display the text box date in php

I have created the text box as well as the query for database.But in my case I want to fetch the data from input field and store it in database.
What actually happens is when I use the function $_POST['launch_date'] it displays the date that I add in db but when I store this in a variable and convert it to y-m-d format it doesn't give me the answer from the text field like $launch_date=date("Y-m-d",strtotime($_POST['launch_date']));.
when i print the both the above mentioned code in single line like echo $_POST['launch_date'] .$launch_date."<br>"; I get the following results
30/01/2020 1970-01-01.
The first one is from my text box and the second one is from the variable that I have created.
Use DateTime::create_from_format() to specify the format of your dates.
$launch_date = DateTime::create_from_format('m/d/Y', $_POST['launch_date'])->format('Y-m-d');
$_POST['launch_date'] = '30/01/2020';
$date = str_replace('/', '-', $_POST['launch_date']);
$launch_date = date('Y-m-d', strtotime($date));
echo $_POST['launch_date'] ." ".$launch_date."<br>";
Output: 30/01/2020 2020-01-30
The issue here is that the / has confused the format of m/d/Y (instead of d/m/Y). And if the date time is not valid (for instance, the month is greater that 12) is in the American format, you'll get the default time (aka UNIX timestamp 0) ie 1970-1-1.
You can create date from user input by
$launch_date = DateTime::create_from_format('d/m/Y', $_POST['launch_date'])->format('Y-m-d');
This is create a date variable of the form Year-Month-Date.
You can change the format as per your requirement.
Y-m-d is the standard date format for MySql.
If You are using PHP version 5.2 or lower you , you will have to parse the date in to 'd'. 'm' and 'y' . Then you will have to create a new date.

How to make mysql accept date in a particular format from a migration page?

I have a bunch of excel sheets which I will be uploading to the database; with date in the format 05-Sep-2019. Is there a way to make mysql recognize it as a date rather than a string? I can use replace in excel to replace '-' with '/' if '-' doesn't work.
Thank you.
You can use strtotime function of php and format as you want
$date = '05-Sep-2019';
$newdate = date('Y-m-d',strtotime($date)); //2019-09-05
$newdate = date('Y/m/d',strtotime($date)); //2019/09/05
$newdate = date('d/m/Y',strtotime($date)); //05/09/2019
Or in Mysql
Kindly Note that MySQL stores date in YYYY-MM-DD format by default.
One way is to convert all dates into YYYY-MM-DD format so that it will be compatible with MYSQL.
We can format the date column in excel before exporting it to MYSQL by following the below steps:
Select the column which contains the date
Right click and select format cells
Choose date under category(on left-hand side)
Then choose custom under category
Under type insert the format --> YYYY-MM-DD
You will see all the dates in the columns get converted into the format --> YYYY-MM-DD
Then you can import the date into MYSQL safely.
Another way is to keep the column datatype as VARCHAR and use TO_DATE() function to parse the string data into DATE format.
you can convert it into Y-m-d format
$date = date('Y-m-d',strtotime($your_excel_date)) // 2019-08-29
$date = date('Y/m/d',strtotime($your_excel_date)) // 2019/08/29

In PHP convert date in to time format output

In my database field there in date and time saved using time(); function.
I want to compare that one with given date in date format like '20/02/2015 '
my query is like:
select view,optin,insertTime,isUnique from sg_page_report as PR where insertTime='20/02/2015'"
where insertTime contains different format date like '1393587636'
How can I resolve this one?
WHERE FROM_UNIXTIME(insertTime)='2015-02-20'
Or you can do it like
$ts=mktime(0,0,0,2,20,2015); // or
//$ts=strtotime('20/02/2015'); // inefficient
And your query can be modified to look like
WHERE insertTime=$ts

convert date data into mysql date format

I want to convert the data on which I have the format
$dateToday = date("d-m-Y");
so the value of $dateToday is 27-12-2012
Then I want to save it to the database with the mysql data type date. How to keep the value of 27-12-2012 it can be stored in the mysql database with the format 2012-12-27?
Help me please. Thank you
Yes, you can convert the date with strtotime();
$dateToday = date("d-m-Y");
$newDate = date("Y-m-d", strtotime($dateToday));
OUTPUT: 2012-12-27
And then you can store data to your database.
When you have to recover the date you can reverse this operation like this:
$dateFromDatabase = "2012-12-27";
$reverseDate = date("d-m-Y", strtotime($dateFromDatabase));
OUTPUT: 27-12-2012
(corrected "Y-m-d" to "d-m-Y" in 2nd date call)
this is how it works:
You have to store your data in the proper mysql format. It will allow you to make whatever ordering, aggregating, filtering and calculating your dates.
But when you need to display your data, you may convert it in whatever format you wish, using mysql DATE_FORMAT() function:
SELECT DATE_FORMAT(dt,'%d-%m-%Y') as dtf FROM TABLE
will give you dtf field formatted in your custom format
i'll show u how to do that.
To explain i create one table called testtable1 it contain only one column called
col1 of type DATE
Table creation query is given below
CREATE TABLE `testtable1` (
`col1` DATE NULL DEFAULT NULL
)
Following query will work as you need.
In the first line i declared a string variable. In the second line i converted that string to your required date format and inserted into table.
set #var1='27-12-2012';
insert into testtable1 values(STR_TO_DATE(#var1, '%d-%m-%Y'))
You could also try to use the mysql function for converting to a date from a string, i.e
STR_TO_TIME
.
The SQL query could be
INSERT INTO foo_table (foo_date)
VALUES (STR_TO_DATE('27-12-2012','%d,%m,%Y'))
If you want to store data in MYSQL table in this format, you need to declare the column as varchar.
Because the datetime store date in a different format like 'yyyy-mm-dd hh:mm:ss'
The output is wrong This cannot show the date from the database .This show 1970/01/01.....
$date=Date($year."/". $month."/". $day);
$date=Date("Y-m-d", strtotime($date));
echo $date;enter code here
Try this
$dateToday = date("d-m-Y");
$dateForMysql = date('Y-m-d', $dateToday));

MYSQL mixing up dates

I am trying to insert a date in my Database which I get from a php input.
The code I am using to insert the value looks like this
$length = strrpos($fristdatum, " ");
$newDate = explode(".", substr($fristdatum, $length));
$fristdatum = $newDate[2] . "-" . $newDate[1] . "-" . $newDate[0];
Lets say I enter 14.12.2012 as the date if I echo $fristdatum I get 2012-12-14 but as soon as I insert it in my MySQL DB it turn to 2014.12.20 any ideas?
The Column Type is date. The insert is somewhat like this
mysql_query("INSERT INTO sch_anschreiben (date)values('$fristdatum'))
there are more values but I guess that doesn't matter
Thanks in Advance!
Well thanks for the help guys i figured it out i used $fristdatum in a array for str_replace ,after i formated it, like this
$patern = array("[Date]")
$words=array($fristdatum)
$content = str_replace($patern, $words, $content);
and after that inserted it in the DB now I changed it so it would format after the str_replace and it seems to work just fine.
also would appreciate if someone could explain me why^^.
Instead of explode and hard coded conversion, prefere using DateTime::createFromFormat if you have PHP 5.3 or later.
$date = DateTime::createFromFormat('d. m. Y',$fristdatum);
echo $date->format('Y-m-d');//echoes 2012-12-14
Now that you correct your script to register your dates the right way, you should ensure your database is good.
You can use this request I think :
UPDATE yourtable SET yourdate=CONCAT(MONTH(yourdate),'-',DAY(yourdate),'-',YEAR(yourdate)) WHERE MONTH(yourdate) > 12
The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.
The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.
The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.
So if you want to store it like 14-12-2012 then use its datatype as varchar.
Convert it into Y-M-D format. You should directly put 2012-12-14 onto your database.

Categories