This is regarding Symfony 1.4, and probably all prior versions, running php 5.3, mysql 5.1.
If I store old dates in a database < 1970..., and I then retrieve them, they are automatically converted into an incorrect date.
Example table:
id, some_date
1, 1961-09-09
A quick example.
$record = MyTablePeer::retrieveByPK(1);
echo $record->getSomeDate();
//returns: 09/09/61
//Want it to return: 1961-09-09
Where is the output format controlled from? I need to get the entire date with the entire year stored in the database.
Date field getters have a $format parameter, use that, like $record->getSomeDate("Y-m-d");.
Also, this is what the docblock says about date accessors:
This accessor only only work with unix
epoch dates. Consider building with
propel.useDateTimeClass or change this
column type to the (deprecated)
"before-unix" column type (e.g.
BU_TIMESTAMP or BU_DATE) if you need
to support pre-/post-epoch dates.
#param string $format The date/time format string (either date()-style or strftime()-style).
If format is NULL, then the integer unix timestamp will be returned.
#return mixed Formatted date/time value as string or (integer) unix timestamp (if format is NULL).
#throws PropelException - if unable to convert the date/time to timestamp.
Related
This question might be stupid but i am really curious on what is the recommended way. I am making demo rest api for my iOS app. I am iOS programmer and getting into learning php to write my own quick demo api.
On one of my table i have column of type TIMESTAMP. I am sending unix timestamp value to my api that will insert that value into that column.
I checked database and default timestamp value format is stored like
2015-04-15 16:48:25
and unix timestamp is like
1430233486
My API receives unix timestamp from my iOS app. Now my question is do i have to convert this timestamp that my app sends to the format that mysql saves ? I need to save data in single format. One more info: Sometimes if timestamp is not sent by app, then mysql inserts the current timestamp itself and this current timestamp is in format 2015-04-15 16:48:25 again.
My preference is to save in traditional unix timestamp format. Is there any settings/query that if mysql decides to store current timestamp in column than its always in 1430233486 format ?
Note: My assumption is that TIMESTAMP type in mysql means unix TIMESTAMP. There are DATETIME type too in mysql. I may be wrong with my assumption as i can simply use INT type for storing my unix timestamps but i like the feature of mysql inserting current timestamp as default without business logic code if i don't provide any timestamp from app.
As far as I know, There's no a dedicated field's type for unix timestamps in mysql. Unix timestamps are actually signed 32bit integer, therefore you can use the INT type to store them.
Please be aware to the range of the INT type field (https://msdn.microsoft.com/en-us/library/ms187745.aspx). I believe that INT would work for you, but just in case you're dealing with off-the-range numbers, you might need to use BIGINT.
In my opinion, you should store the unix timestamp rather than the formatted date/time option since it's easy to manipulate, for calculations or reformatting.
I recommend you to don't use 2 different types of data into the same column of database, is not data consistent.
I recommend you to do a function or method that compares if the value is a string or a number and then convert it to a datetime or timestamp.
Like for example:
function convertTime($value) {
if (is_numeric($value)) {
return date("Y-m-d H:i:s", $value);
}
return $value;
}
Or to convert from date to unix timestamp:
function convertTime($value) {
if (is_string($value)) {
return strtotime($value);
}
return $value;
}
and use that function to everywhere.
I'm grabbing data from a webpage that returns a timestamp in the following form (which is a string):
2013-11-09T15:14:48.957604
How can I interpret this in PHP, and what is the best way to store this in a MySQL database?
The best way to store it is to use the MySQL DATETIME data type. It is specifically meant to handle date/time values, and it works beyond the year 2037, which is the approximate limit using unix timestamps. MySQL and PHP both handle these values with ease using built in functions/libraries (for PHP, see the DateTime class as mentioned by another commenter).
http://dev.mysql.com/doc/refman/5.5/en/datetime.html
http://php.net/manual/en/class.datetime.php
I just want to expand on the information given here.
This will convert the string into a DateTime object.
$dtObject = new DateTime("2013-11-09T15:14:48.957604");
But, in order to store the timestamp in the database there are several requirements.
Needs to be in Y-m-d H:i:s format.
MYSQL column type must be datetime/timestamp
Needs to be a string.
Now in order to get this timestamp into a MYSQL friendly format we need to use the format function.
$timestamp = $dtObject->format('Y-m-d H:i:s');
You can now INSERT this into the database. You can also output the same way following these date formats.
I am using this code successfully, but is there a way to run this where I don't need to convert my variable from a string to a number?
I am confused and wondering if I am doing it efficiently because the mysql timestamp datatype is a string but the php timestamp is a number. There should be a date format conversion for the mysql timestamp,no?
date(DATE_ATOM, strtotime($program->start_time))
the date function takes a timestamp long datatype but mysql stores timestamps in a datetime string format. Is there another way to do this where I just convert once?
I don't know why do you need to change the dates in your code.
In Laravel base Model, dates works as Carbon objects. When that fields are stored in database, they're mutated to database date format. That's because date fields have their owns getter/setters in base model.
You don't need to change your custom date fields formats, only declare them in the $dates Model propertie so it can mutate that fields when they are stored in database.
For example, in this User model class the attribute start_time will be mutated from Carbon to MySQL format when it will be saved.
Class User extends Eloquent
{
protected $dates = ['start_time'];
}
You can read more about that here http://laravel.com/docs/4.2/eloquent#date-mutators
I expect that would help you to understand it better.
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.
I currently have a date that's being stored in my SQL database as a VARCHAR of 255 characters. I declared this string as
//within an object...
$date = date(DATE_RFC822);
Now, later on in the coding, I realise that I need to actually compare dates with each other. My initial, very naive attempt looked a little bit like this:
if(object_1->date > object_2->date){
//do this that assumes that object_1 was created at a later date than object_2
}else{
continue;
}
While this worked fine for different times of the same day; using the code a week later began to show significant bugs.
strtotime() converts a string into unix time (an integer) which can easily be compared with other unix time values. If you are running PHP 5.2.8 or greater, you could also make use of the DateTime class which are relatively easy to compare (see this question for more info)
You can't compare dates in DATE_RFC822 format with each other. You should use Date or DateTime fields in MySQL and DateTime or Unix timestamps in PHP. It's safe to use your DATE_RFC822 string in the PHP DateTime constructor or in strtotime(). (Still, if you use Date or DateTime in MySQL you can also sort by date and search by date, etc.)
PHP DateTime objects can be compared with each other like normal PHP variables, so you can do $date1 < $date2 to determine if $date1 is before $date2.