How to pass value from date range picker to SQL? - php

I faced two problems here:
(A) How do I pass the value of start date and end date from this http://www.daterangepicker.com/#example4 into query *select * from table_name where date between 'start date' and 'end date'*
(B) My data consists of date within 2018/04/11 to 2018/04/13,which means it also include these two dates. How to ensure the selected date range can be displayed in the query result?

first of all, sql stores them using different format (by default) so you will have to convert it, maybe by using strtotime() and date() combination to get the wanted SQL format.
then you can use BETWEEN WHERE clause to filter values like:
WHERE created_at BETWEEN 2018-04-11 AND 2018-04-13

First extract the dates via explode:
$dates = explode(" to ", $datesFromDatepicker);
Then create the start and end date:
$start= DateTime::createFromFormat('Y/m/d', $dates[0]);
$end= DateTime::createFromFormat('Y/m/d', $dates[1]);
In your query use:
$start->format("Y-m-d");
$end->format("Y-m-d");

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.

Compare oracle date in query with PHP

I want to compare dates retrieved from oracle database in php. but i don't know how to convert month name like Sep to SEP. I don't know how to convert month in capital letters. I want to convert 2015-09-01 formate to 03-SEP-15.
//sDate: 2015-09-01 eDate: 2015-09-03
$date1=date('d-M-Y', strtotime($sDate));
$date2=date('d-M-Y', strtotime($eDate));
// CREATEDATE: 03-SEP-15 02.44.42.000000 PM
QUERY:
$stid = oci_parse($conn, "SELECT * FROM table1 WHERE CAST(CREATEDATE AS DATE) between '".$sDate."' AND '".$eDate."'");
ERROR:
Warning: oci_execute(): ORA-01861: literal does not match format string
In Oracle you can convert a string into a date using the to_date() function. And a date to a string using the to_char() function
Example
to_date('01-JAN-2015','DD-MON-YYYY')
will return a value of type date.
Now, in your query you must make sure that the values in your "between" statement are date values. The only way to ensure that is to make $sDate a atring value and use the to_date function.
Assuming that your date strings are in format 01-JAN-2015...
$stid = oci_parse($conn,
"SELECT *
FROM table1
WHERE CAST(CREATEDATE AS DATE)
between to_date('".$sDate."','DD-MON-YYYY')
AND to_date('".$eDate."','DD-MON-YYYY')");
See also the Oracle documentation on date formats.
http://docs.oracle.com/cd/B28359_01/server.111/b28286/sql_elements004.htm
So you have a date string like this: '2015-09-01' and you want to convert it to '01-09-2015' to be used in a query.
If you want to debug if the issue is in how you handle the date conversion in php try to do:
list($year,$month,$day)=explode('-',$date);
$months = ['','JAN','FEB','MAR','APR','MAY','JUN','JUL','AUG','SEP','OCT','NOV','DEC'];
$date = implode('-',$day,$months[$month],$year);
Then try to run the query. You don't need to take the string, convert it into a date (unix timestamp) and convert it back to a string if you just need to change the order of the elements in the date. You have to follow your approach if you want to manipulate your date adding or removing some time or stuffs like that.
If the query is still in error and you are sure that dates in your db are stored with that pattern than the issue is in the query syntax

Formatting the time hh:mm:ss.0000000 to hh:mm not working

I am using MsSQL in PHP. I am storing the time in database table row with datetime as datatype.The time is stored like this :08:30:00.0000000.I need the time to be displayed in 08:30.I have used
date('h:i', $time_value); // $time_value stores the time value
This formats the date and gives the result in 4:00. Any formatting is required to display the correct time stored in database?
The PHP date function does not expect a Mysql datetime as second parameter but you do so:
date('h:i', $time_value);
^
|
second parameter
This is the reason why it does not work. You're using the wrong value, convert the database value into a timestamp first because the date function needs a timestamp. As you migh imagine, this has been done before, here is just a selection of related Q&A material:
MySQL convert datetime to unixtime?
MySQL convert datetime to Unix timestamp
Alternatively just use a string function like substr to obtain the string you're looking for:
$time_value = '08:30:00.0000000';
echo substr($time_value, 0, 5); # 08:30
Demo: https://eval.in/146148
You can do the conversion / formatting with the SQL statement already, a related example is given in:
How to display time in HH:MM format?
You can also use:
date('h:i', strtotime('08:30:00.0000000'));
In your case:
date('h:i', strtotime($time_value));

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));

Convert string to date in mm-yyyy format in php

I have $year and $month as two variables. I want to convert them into date so that I can insert it into mysql table. I just want to store mm-yyyy in mysql.Is it possible in php?
$date = "$year-$month-01";
If you plan on storing it in a real 'date' field, you must have a day of the month too.
MySQL displays DATE values in ‘YYYY-MM-DD’ format.
So if you want it as a DATE, you have to put a day in your date like : $date = "$year-$month-$day".
If you don't want it as a DATE, you can store it as a 'MM-YYYY' like that : $date = "$month-$year".
After, you just have to make your MYSQL Query and it will work.

Categories