codeigniter get timestamp from database and format - php

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.

Related

Insert date in specific format into MySQL db

I need to insert the current date in the following format into a TIMESTAMP column in a MySQL db: d-m-Y
As of now I am using SQL NOW(), which returns the date as Y-m-d. Because I am using AJAX to display the data I cannot format the returned result using $date_returned->format(d-m-Y). Therefore I need to insert the date in the format that I will display on my AJAX call.
I tried to insert the date using the following functions:
1) date('d-m-Y');
2) (new \DateTime())->format('Y-m-d');
I understand these two functions do pretty much the same thing but I was not sure what else I should try.
MySQL threw the following error for both dates:
Error : (1292) Incorrect datetime value: '-2014' for column 'msg_date' at row 1
I am guessing this should be an easy fix but I can't figure out what is wrong.
I tried both TIMESTAMP and DATETIME on MySQL's end but neither worked. (I need it to be TIMESTAMP though).
Any suggestion is welcome!
$newdate= date('Y-m-d', strtotime('10-09-2015'));
or if you want current time just use
$now = date('Y-m-d');
If your msg_date column's structure is DATETIME or TIMESTAMP, the date format should be:
YYYY-MM-DD HH:MM:SS
which can be formatted through PHP like this:
$date = date("Y-m-d H:i:s");
Or if you already have a date, and you want it to convert to that format, we can use strtotime():
$date = date("Y-m-d H:i:s", strtotime($date));
For more date format, check this link.
The MySQL error message indicated that you had the date format the wrong way around.
Year must go first, then month, then day, as in:
date('Y-m-d') // right
In your first example, you have
date('d-m-Y') // wrong
In one of your examples above, you have it right, but you say you got the same response, so I assume that was not what you actually tried.
Another thing to note is that a MySQL TIMESTAMP column stores both a date and time. It's valid to give MySQL just a date (MySQL will just leave the time at zero), but if you have no need to store a time, you may as well make the column DATE instead of TIMESTAMP.
If you want to display your dates as d-m-Y then by all means do so, but they need to be sent to MySQL as Y-m-d.

Trying to convert mysql timestamp to time using strtotime

strtotime($_SESSION['starttime'])
I tried to convert the mysql time stored as $_SESSION['starttime'] of value 1422094831 but I want to convert the resulting to time using strtotime but returns no value. What could be wrong?
strtotime() is used to create a timestamp, not convert it to a date. If your session variable stores a timestamp, try using the date() function to convert it to a more readable format.
echo date("Y-m-d", $yourTimestamp); //Will print YYYY-MM-DD format date
The PHP doc will help you using the date() function correctly.
If that's not what you want to do then please provide more information in your question, from what I understand you already have a timestamp.

How to insert PostgreSQL timestamp with Laravel model

I'm using Laravel 4 with PostgreSQL. I created PostgreSQL timestamp fields with a migration:
$table->dateTime('start_date');
I tried inserting a timestamp generated with strtotime and received an error:
Invalid datetime format: 7 ERROR: invalid input syntax for type timestamp: "1383321600"
Hacking around it works:
new MyModel(array('start_date' => DB::raw("to_timestamp($start_date)")));
It seems like there should be a better way. Laravel itself generates timestamps without issue for the created_at and updated_at automatically-populated dates.
You must convert whatever the outcome of your strtotime is to the MySQL format before trying to save it. You can do this using the date function.
Example
date('Y-m-d H:i:s', strtotime('some time'))
It seems that Unix timestamps created with strtotime aren't converted correctly, but true PHP DateTime objects work fine.
I switched to using date_create with the same arguments and I can insert it directly without using DB::raw.
Doc for strtotime() says
The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp
In SQL databases generally, timestamp doesn't mean a Unix timestamp. In SQL, if you were going to insert a timestamp into a table using standard SQL, you'd do it like this.
-- Implicit use
insert into table_name (column_name)
values ('2013-01-01 08:00:00');
-- Explicit use
insert into table_name (column_name)
values (timestamp '2013-01-01 08:00:00');

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?

Categories