Conversion of date to integer PHP - php

I have two tables, in both of them i have fields to store dates.
But in the first it stores the date as an integer and in the second like in time format.
I want to save the value from date format to integer, but it does not convert the date to int.
I tried the function idate(), but it converts only one char to parameter.
The date looks like string(19) "2006-08-09 13:22:44".
Can somebody help me?
Maybe strtotime() this my solution?

For PHP conversion use this:
$timestamp = strtotime($mysqltime);
echo date("Y-m-d H:i:s", $timestamp);
And for pure MySQL conversion:
SELECT UNIX_TIMESTAMP('2007-11-30 10:30:19');
Here, you can change the date string with your table field name (SELECT UNIX_TIMESTAMP(fieldName))
For reference:
https://stackoverflow.com/a/4577805/2883841 - the two answers with most votes.

Convert date from second table into Unix Time Stamp and save it into first table as int
EDIT
I think you should not do this conversion unless no other option left

Try UNIX_TIMESTAMP() e.g.
select UNIX_TIMESTAMP('2006-08-09 13:22:44')
1155126164

If your fieldtype is INT and you can't or don't want to change that to DATE or DATETIME, you should save the date as timestamp. In that case, strtotime is your function.
Instead of converting by php strtotime() you could convert the fields directly in mysql:
UPDATE table1 SET `dateInt`=UNIX_TIMESTAMP(table2.`dateDate`) WHERE table1.`id`=table2.`id`

Related

How can we convert date stored as varchar to date

Here my date values are stored in varchar and i want to get results while searching in between dates
Here is my code please have a look
public function get_report($from1,$to1)
{
$from=DATE($from1);
$to=DATE($to1);
$this->db->order_by('customer_account_id','desc');
$this->db->where('Date(`date`) >= ',$from);
$this->db->where('Date(`date`) <= ',$to);
return $this->db->get('customer_accounts');
}
my date_format is like this 31-01-2017 and when i use my code like this the result is not obtaining
MySQL date function expects dates in 'YYYY-MM-DD' format. If you change the date values that are stored in varchar column to be in that format then you should get the desired results. It will be better still if you convert the varchar column to be date as this will be faster when you select data from the table.
Use mysql CONVERT function to convert value from varchar to date while fetching.

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

PHP mysql: how do I select records for today, or a particular day?

I am working with a table on which I can't change the structure.... Now there is a varchar column which contains a timestamp. Now I need to select the records whose timestamp translates to the current date, or a specified date.
Any help?
First off you shouldn't be storing date information in a mysql database with a VARCHAR field. Rather use DATETIME that is what it is for. I can only guess how you have stored your timestamp date in the database but I am going to assume it is the following format:
YYYY-mm-dd hh:mi:ss (ie '2011-04-15 09:23:55')
You now have to format your input which I am assuming is a time object or it is a string in the same format as the data in the database:
$yourdate = strftime("%Y-%m-%d", $input);
then construct your query
query = "select * from table where substring(datecol, 1, 10) = '$yourdate'";
execute this and you should be good
Based on the format that you're storing the date as a string, use the STR_TO_DATE function to parse out the date. Then you can use it as a part of the where clause to query desired data.
try this
select * from table where date(your_field)=CURDATE()
or specific date
select * from table where date(your_field)=DATE_FORMAT(2011-05-31 00:02:00, '%Y-%m-%d')

PHP convert date from a MySQL query

I think this is a simple question. We have a MySQL database with a DATE field, the date is stored in US format (2010-06-01).
In my PHP page where I'll display the date, I simply want to convert this date into a UK format (01-06-2010).
Any help and advice appreciated!
Thanks,
Homer.
You didn't specify and your example is ambiguous, but I'll assume you're talking about outputting in day-month-year format. You can use strtotime to parse a string date into a unix timestamp, and then give that timestamp to date along with the format that you'd like to output your date in:
date("d-m-Y", strtotime($date_from_mysql));
The manual page for date lists all the formatting options that you can give. In this case, d represents the day as a zero-padded number, m represents the month as a zero-padded number, and Y represents the year as a four digit number. Any characters that don't have a specific meaning for date come across as-is.
You can do it right from mysql using DATE_FORMAT() function.
example : "SELECT DATE_FORMAT(date_column,'%d-%m-%Y') as my_formated_date;" will do what you need , just make sure to use in fetch method my_formated_date column
You can parse the date with this function:
http://php.net/manual/en/function.strtotime.php
It will return an integer which is number of seconds since 1970 (called a Unix timestamp).
Then use this function to format that number any way you like:
http://ca3.php.net/manual/en/function.date.php
You can also create a Date object in PHP using the DateTime::createFromFormat feature.
<?php
$date = DateTime::createFromFormat('Y-m-d', $sql_date);
echo $date->format('d-m-Y');
?>

Convert Date Format

I have a moodle installation in which there is a column in mdl_user table called firstaccess whose type is bigint(10) and contains date in following format 1266839570.
I am writing a query for accessing users according to date filters. For e.g. i want to check which users firstaccess is greater than '2010-04-12'. How can i convert the date? These two date formats are different. I think firstaccess is unix timestamp. Should i change the '2010-04-12' into unix timestamp or there is a way to convert firstaccess i.e 1266839570 to yyyy-mm-dd format.
Please help me on this.
Thanks
You can create a unix timestamp in php with the mktime() function, then simply put it in your query.
MySQL has a date_format() function, that can format dates however you like, but I'm not sure if it works with bigints. You'd better go with the mktime.
date() and mktime() are functions to concert from unix timestamp and back.
You can convert your dates in either way
I believe you can write your query using a timestamp. Eg.
SELECT * FROM mytable WHERE firstaccess >= TIMESTAMP('2010-04-12')
http://dev.mysql.com/doc/refman/5.0/en/timestamp.html
I don't know what form the date in your form is, but you can easily convert it to a timestamp (if it already isn't one) using mktime. For example:
$mytimestamp=mktime(0,0,0, $month, $day, $year);
Then just add it to your query:
$myQuery= "SELECT whatever FROM sometable WHERE " . $mytimestamp . ">=firstaccess";
Like Paul Peelen, my answer is a MySQL query. I'm going the other way, though, and converting first access into a date.
Using the date information in your problem:
SELECT * FROM mytable WHERE DATE_FORMAT(FROM_UNIXTIME(firstaccess), '%Y-%m-%d') > '2010-04-12';

Categories