Date to Timestamp Help required - php

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

Related

How i did not added 20 before date in mysql

I have issue with this date m/d/y for example 01/09/15 but when i insert that in my table of field name date and declared date i adde to me 2001/09/15 how can i save this format 01/09/15 in field database of my table
You can't change the format of mysql dates. You could save your date as a varchar but you can't use any manipulation.
You can save it as a mysql date or a time int, and then cast it to the format that you want, using date function.
You can read more about this in this post:
Change date format (in DB or output) to dd/mm/yyyy - PHP MySQL
I hope this will be useful.

get month & year from Mysql Date format m/d/y & compare with today's month & year

I have mysql date format m/d/y as varchar like 03/12/2015 for 12 March 2015. Now I want to count all the result from mysql table in which the month & year are the same as of today's date.
I am using following query, but no sucess
SELECT * FROM student where DATE_FORMAT(regd_date,'%m/%y')=DATE_FORMAT(now(),'%m/%y')"
Thanks for any help
You will first need to use STR_TO_DATE() to convert your varchar column to a date before using DATE_FORMAT().
SELECT * FROM student
WHERE DATE_FORMAT(STR_TO_DATE(regd_date, '%m/%d/%Y'),'%m/%y') = DATE_FORMAT(now(),'%m/%y')
Adjust the format for STR_TO_DATE() accordingly.
Note: I would discourage storing dates as varchars.

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

Getting a Date Range in MySQL form a UNIX Timestamp in PHP

I have a UNIX timestamp in a table like 1321521158 and want to get the details from a table for the particular date, like:
$mydate="11/11/2011";
SELECT notes FROM table WHERE `addeddate`=$mydate
How do I get it?
you can use the mysql date function like:
WHERE date(addedate) = date(mydate)
or you can calculate the starting and ending timestamp for the given day you want to know and then do
WHERE addeddate >= mystarttimestamp AND addeddate <= myendtimestamp
try
WHERE $mydate = FROM_UNIXTIME('dateColumn')'%Y/%D/%M');
MySQL Date Time Functions

How could I insert date from mktime into MySQL?

I want to insert date in format m-d into database. I used this code
$date1=date("m-d",mktime(0,0,0,$month1 ,$day1,1));
The date is inserted as 0000-00-00 in MySQL, and I don't know why.
I want to do that to compare that date stored in MySQL with current month and day. How can this be done?
mysql date must be of the form Y-m-d . Building a date with m-d form brings an invalid date.
If you want to compare only month or day, you can use mysql MONTH and DAYOFMONTH functions. For example:
$query = "SELECT * FROM table WHERE MONTH(datecol) = {$month1} AND DAYOFMONTH(datecol) = {$day1}"
$date1=date("Y-m-d",mktime(0,0,0,$month1 ,$day1,1));

Categories