I have a datepicker on my site. When you select a date, it displays as dd/mm/yyyy
In order to search the date, the mysql db has to be similar to it
But it looks like yyyy-mm-dd H:i:s which I cant search for
Edit
I also wanted to search between two dates but was having difficulty since the two formats were different
My question was, how would I format my search function to find a similarity from the datepicker and the mysql datetime?
Here's what I have so far (date_avialable is the column name)
$sql = mysql_query("SELECT * FROM search_v WHERE Currency='$currency' AND rout_to='$sto' AND
rout_from='$sfrom' AND DATE_FORMAT(date_avialable,'%Y-%m-%d') BETWEEN '$sfromda' AND 'stoda' LIMIT 10") or die(mysql_error()) ;
while ( $runrows = mysql_fetch_array($sql))
I wanted to do something like this
$mysqldate = date( 'Y-m-d H:i:s', $phpdate );
$phpdate = strtotime( $mysqldate );
But didn't know how to call that into the db
If your specification in question are correct try (to change) this in your code..
AND DATE_FORMAT(date_avialable,'%d-%m-%Y')
or separate your PICKER date to strings shown bellow on the left and do this
AND ($calendarday) = date_format(from_unixtime(date_avialable),'%d')
AND ($calendarmonth) = date_format(from_unixtime(date_avialable),'%m')
AND ($calendaryear) = date_format(from_unixtime(date_avialable),'%Y')
If that aint gonna work, please go to phpmyadmin, and try to tell us what is the format of date in your "date_avialable" row. Is it a unix timestamp or d-m-Y or similar..
Related
In my site, I have a bootstrap datepicker which allows user to pick date in format of MM/DD/YYYY (e.g: 05/12/2014). Then when this data is submitted, I used the following PHP code to convert it into Datetime type, then insert into start_date (DATETIME datatype) column in MySQL .
$start_date = date('Y-m-d', $_POST['start_date']);
the insert query in PHP does nothing with reformatting the date. It just simply insert into corresponding column.
However, instead of inserting '2014-05-12', the value inserted into database is '1970-01-01'. That's so weird to me. Can anybody tell me what's wrong here. Is this that I used incorrect PHP function or incorrect timezone setting or ...
Just do this:
$start_date = date('Y-m-d', strtotime($_POST['start_date']));
You could also use strtotime() on your $_POST.
$start_date = date('Y-m-d', strtotime('05/12/2014'));
try to use
$date = str_replace('/', '-', $_POST['start_date']);
$start_date = date('Y-m-d', strtotime($date));
For more :- Converting between illogically formatted dates (changing /slash/ to -dash- )
This question already has answers here:
Change MYSQL date format
(4 answers)
Closed 9 years ago.
I've got a piece of code which involves dates but wanted to change the format of the date; it has been set (as default by MYSQL) to YYYY/MM/DD however I want it so that it is in the form DD/MM/YYYY. How can I change this?
date( 'd/m/Y', strtotime( $date ) );
This should work although are you sure mysql isn't defaulting to yyyy-mm-dd as is the norm
UPDATE FOR COMMENT
$date = "2013-03-01";
date( 'd-m-Y', strtotime( $date ) );
Output: 01-03-2013
UPDATE FOR COMMENT 2
$loan_date = date( 'd-m-Y', strtotime($_POST['loan_date']));
$return_date = date( 'd-m-Y', strtotime($_POST['return_date']));
echo $loandate;
echo $return_date;
Working example in this case I just replaced the post vars with example strings but shows it works. If its not working then check your input strings but it should work with ANY format date stamp.
http://phpfiddle.org/main/code/viu-ghp
If you are planning to update the value of the column because of different format, leave it as it as it is already DATE. It is better to save date as DATE that VARCHAR.
However, you can change the format during the projection of the column (SELECT statement)
SELECT DATE_FORMAT(colNAME, '%d/%m/%Y') as newDATE
FROM tableNAme
Using the DateTime class rather than the PHP's older date handling functions:
$dateFromSQL = "2013-03-01";
$dateObj = DateTime::createFromFormat($dateFromSQL, 'Y-m-d');
print $dateObj->format('d/m/Y');
I've looked at several of the threads that have to do with this issue and still can't figure out what's going wrong with my code.
I'm bring a date field from my form that is in this format "mm-dd-yyyy", when I bring it to my code to update the date of birth field as "yyyy-dd-mm" I use the following
$birth=$_POST['dateBorn'];
$dateB = date('Y-d-m',strtotime($birth));
$finaldateB = ($dateB === false) ? '0000-00-00' : date('Y-d-m',strtotime($dateB));
When I echo the values of the variables (using a date of birth of 11-23-2012) I see the following values for them birth = 11-23-2012, dateB = 1969-31-12, finaldateB = 1969-31-12
I'm obviously doing something incorrect and haven't been able to locate where
TIA
In order to get things to function, I've at least discovered a work-around that may not be elegant but gives me the results I need for now
$dateB = $birth;
$dateborn_a=explode("-",$birth);
$yearborn = $dateborn_a[2];
$dayborn = $dateborn_a[1];
$monthborn = $dateborn_a[0];
$dateOfBirth=$yearborn."-".$monthborn."-".$dayborn;
And then use the value in $dateOfBirth to update the table. It's worked for all the records I've tested it on so far.
$dateB = date('Y-d-m',strtotime($birth));
here
y is for year
d is for day
m is for month
so formate will be yyyy-dd-mm
try
$dateB = date('Y-m-d',strtotime($birth));
You're using Y-d-m instead of Y-m-d. MySQL's date format should be YEAR-MONTH-DAY. Using
$dateB = date('Y-m-d',strtotime($birth));
should fix it.
I cannot seem to find any info on this..
I need to convert a string to a date so that it will import properly to an SQL DATE field. When I import 12/25/2012 to the DB, it appears as 0000-00-00.
What's the proper way to do this?
Links and refs appreciated.
MySQL accepts dates in this format YYYY-MM-DD either change your date format 12/25/2012 to 2012-12-25 or modify them to match the correct format.
EDIT
If you want to continue using your own format try this
list($d,$m,$y) = explode("/", "12/25/2012"); //replace 12/25/2012 with your date
$hyphenDate = $y . '-' . $m . '-' . $d;
echo $hyphenDate;
As #Ravi pointed out in his answer, MySQL accepts dates in the format YYYY-MM-DD. Quoted from 11.1.5. Date and Time Types1:
Although MySQL tries to interpret values in several formats, date
parts must always be given in year-month-day order
For this, you can use str_todate()2 function to format it:
str_to_date('12/25/2012', '%m/%d/%Y);
SQL Fiddle Demo
This way, these input strings will be stored in your database as date objects(without any specific date format). Later, if you want to output these dates in a specific format you can use DATE_FORMAT3 to format it. Something like:
SELECT DATE_FORMAT(datefield, '%Y-%m-%d') FROM Test;
--2012-12-25
1, 2, 3: Links and refs, that you asked for.
Use class DateTime. Examples:
$SomeDate = new DateTime();
echo $SomeDate->format( 'Y-m-d H:i:s' ); //Must be MySQL compatible (YYYY-MM-DD)
$ThisDate = new DateTime( date( 'Y-m-d H:i:s' ) );
echo $ThisDate->format( 'Y-m-d H:i:s' ); //Must be MySQL compatible (YYYY-MM-DD)
I have in a MySQL table a DATE column that represents the date in this format: YYYY-MM-DD.
I wanto to retrieve the date from the database using PHP but display it like this: DD Month, YYYY.
From '2009-04-13' to '13 April, 2009' for example.
Witch is the best way to do it?? ( I know how to get the date from the DB. I only need to know how to convert it)
I also need to display the month names in Spanish. There is a way to do it without translating each month using strplc or something like that??
I'm new to programming, please be detailed.
Thanks!!!
Refer to DATE_FORMAT() function in MySQL. I guess that's the best way for you to do it.
Also, you can make this:
Fetch your date from DB
Use strtotime in PHP, to convert to unix time
Then format the time using date.
By using date() you'll be able to get months names in Spanish when you set your locale in PHP with setlocale.
You could also skip the strtotime() part by using UNIX_TIMESTAMP(date) in your MySql select. But remember that this is a MySQL specific function and may not be be portable in the future.
Execute following MySQL queries:
SET lc_time_names = 'es_ES';
SELECT DATE_FORMAT(t.date,'%e de %M, %Y') FROM your_table t ...
With MySQLi it'll be:
$mysqli->query("SET lc_time_names = 'es_ES'");
$stmt = $mysqli->prepare("SELECT DATE_FORMAT(t.date,'%e de %M, %Y') FROM your_table t ...where id = ?");
...
Another option not yet mentioned:
SQL:
SELECT UNIX_TIMESTAMP(date) FROM table
PHP:
print date('your format', $timestamp_from_the_db);
Personally, I like to use integer data types in MySQL for date storage in the UNIX timestamp format. I leave all the processing of that integer up to PHP. Keeping tables and queries as simple as possible has always served me well. Predominantly, in the code I write, dates have some sort of calculation done to them. This is all done on the PHP side and always in the UNIX timestamp format. Storing or retrieving the dates in anything other than the UNIX timestamp format just means another step for errors to creep in and makes the query less modular. How a date is formatted is best left up until the last minute before it's displayed. It's just my opinion, but unless there are extreme circumstances where you can't process the DB value after extraction, a date shouldn't be formatted SQL-side.
A simplified example:
<?php
$date = now();
$dueDate = $date + 60*60*24*7; // One week from now
$sqlInsert = "INSERT INTO reports SET `dueDate` = $date";
$resInsert = mysql_query( $sqlInsert );
$sqlSelect = "SELECT `dueDate` FROM reports";
$resSelect = mysql_query( $sqlSelect );
$rowSelect = mysql_fetch_array( $resSelect );
$DB_dueDate = $rowSelect['dueDate'];
$daysUntilDue = ( $DB_dueDate - now() ) / 60*60*24;
$formattedDueDate = date( "j F, Y", $DB_dueDate );
?>
The report is due on <?=$formattedDueDate?>. That is <?=$daysUntilDue?> from now.
Simplest way is to use the strtotime() function to normalize the input to UNIX timestamp.
Then use the date() function to output the date in any format you wish. Note that you need to pass the UNIX timestamp as the second argument to date().
This will help you to convert as you want:
$dob ='2009-04-13';
echo date('d M Y', strtotime($dob));
$origDate = "2018-04-20";
$newDate = date("d-m-Y", strtotime($origDate));
echo $newDate;