time to date converstion - php

in our database, we have a column called ctime containing datetime in this format '1408097567'
I'd like to know how to convert this to date format like 'mm/yy/dd' using mysql or php
thank you very much I appreciate your help

Since the format is a UNIX Timestamp you can use php's date function:
$time = '1408097567';
$time = date('M/Y/d', $time);

Use FROM_UNIXTIME()
SELECT FROM_UNIXTIME(ctime) as ctime
FROM your_table
If you need a specific date format use DATE_FORMAT() too.

Related

Conversion of date to integer 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`

How to save data in a different format in MySQL database

I notice that MySQL save date as 0000-00-00 format. But in my application I have validate date for this 00-00-0000 format. I notice that date is not saving properly as different formats. Can I change mysql data format to 00-00-0000 format?
The 00-00-0000 format is not entirely clear; it could be dd-mm-yyyy for instance.
You can simply convert the date you have like so:
$mydate = '24-12-2012';
// create date object by using a known format
// see manual page for all format specifiers
$d = DateTime::createFromFormat('d-m-Y', $mydate);
// format date object in yyyy-mm-dd
echo $d->format('Y-m-d');
See also: DateTime::createFromFormat() DateTime::format()
You can also use MySQL str_to_date function
INSERT INTO yourtable (datefield) VALUES (str_to_date('01-02-2007', '%d-%m-%Y'));
As #Jack explained it is one of the correct way you can choose and
at the time of select date from database you can use
DATE_FORMAT(date,format).
You can't.
Instead of that you have to convert your custom format to database format and back.
To store it in the database you can use explode PHP function.
To when selecting, you can format mysql date format using DATE_FORMAT mysql function.

How to deal with DATE-type in MYSQL & PHP?

I'm building a website with php and i'm using the DATE-type in my MYSQL table to store dates. The problem that i have is that this stores the dates by default in the format YYYY-MM-DD. But i need this format DD-MM-YYYY to appear on my PHP page with the possibility of calculating the amount of days between 2 different dates. How can this be achieved?
That's a display problem. Use mysql's date_format() function to convert to whatever your display requirements are, e.g.
SELECT date_format(datefield, '%d-%m-%Y'), ...
You can use strtotime to convert a string representation of the date to an actual date object in php, then use the date function to spit out the date as any string format you wish. Also, you can be strtotime to perform date calculations. Additional information can be found at this blog post.
$phpDate = strtotime($stringDateFromDb);
date('d-m-y', $dateFromDb);
strtotime('-3 days', strtotime($stringDateFromDb));
Here is an example for a way to do it:
$date_str = '2012-05-20'; //you get it from db
$date_now = strtotime($date_str); //convert it to unix timestamp
$yesterday=$date_now-24*60*60; //make calculations
echo 'yesterday was: '. date('d-m-Y',$yesterday); //date() returns the date in format you need
Further example here: How to calculate the difference between two dates using PHP?

codeigniter get timestamp from database and format

I have a MYSQL table column of type timestamp and using CURRENT_TIMESTAMP as default when a new record is added.
The date field in my database table looks like this 2011-08-16 12:09:25 How is it possible to format that into MM/DD/YY to display on my site? I tried some functions from the date helper but I get errors.
note: I'm trying to figure out how to use Codeigniter functions for this, if possible.
In the CI manual there are examples with $timestamp = '1140153693'; but my database timestamp is a different format and get errors.
To display a timestamp, just use the PHP Date class. But since MySQL outputs timestamps as a string not an INT, you'll have to first convert the timestamp string to a timestamp INT using PHP's strtotime function. The code looks like this:
echo date("m/d/y",strtotime($timestamp));
Date
http://www.php.net/manual/en/function.date.php
StrToTime
http://php.net/manual/en/function.strtotime.php
Sorry, I didn't properly explain the issue but solved it.
In order to use the Codeigniter date functions I had to convert the mysql ISO 8601 date format to a 32-bit integer.
I had to do the following
$unix = mysql_to_unix($row->message_date); //conversion to string
$datestring = "%m/%d/%Y";
echo mdate($datestring, $unix);
I was using the ISO 8601 format and getting errors.

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