How would I do the mysql timestamps in php? - php

Ok so I have a mysql database and I need an updated_at field and a created_at field like ruby on rails has by default. I was thinking of using a timestamp for the updated field and a datetime for a created at field. I was reading this article to help me choose but i still dont know if there is a standard that is followed that will either do this automatically with automatically or with little extra code...any ideas on this

The best practice would be to use UNIX Timestamps on both.
To have UNIX timestamp in PHP, use the
time();
function, and to have it in MySQL use the
UNIX_TIMESTAMP();
function. PHP can simply convert UNIX timestamps to any formatted date string using
Date();
function. And you can also make calculations with that.

Well the code from php will only provide you with your time as your computer see's it. Where mysql it has the CURRENT_TIMESTAMP.

You could do it entirely in your database management system. For example, MySQL has functions to use the current date or time: http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Example: insert into a (CURRENT_TIMESTAMP);

You are on track with using TIMESTAMP for your updated field which provides the functionality you are looking for. To keep your date format consistent, I would then use DATETIME for your created field and use now() on insert.
I prefer UNIX timestamps, but then you have to code for the updated_at field.

Related

Storing timestamp in MySQL

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.

Can someone please clarify how MySQL's TIMESTAMP is used in conjunction with PHP's DateTime class?

I've been studying the differences in usage between MySQL's DATETIME and TIMESTAMP. It seems that it's pretty straight forward with DATETIME. I would use the following procedure:
Choose the default timezone for all dates, such as UTC.
Let user select a date from drop-down.
Create new PHP DateTime object with the chosen date, using the user's timezone settings, such as EST.
Convert the object to UTC, and insert into database.
On another page, retrieve datetime value and make a new DateTime object with it, using UTC timezone.
Convert object to user's local time (EST), and display to him.
Now, it seems that mysql's TIMESTAMP column type can help eliminate all of these conversions.
What would the above steps look like with the TIMESTAMP column type?
Would I need to do "SET time_zone = timezone;" in the beginning of each pageload to set the timezone to the location of the user?
Would it be easier to ONLY use one type of date column type per database? If not, it may require two different sets of functions to produce the right date.
Should TIMESTAMP only be used in columns not intended to be shown to the public (so as not to deal with formatting)? Like when a row was created, last edited, etc.
I have not tested any of this approach, but it seems pretty straightforward =)
You shouldn't need to convert dates, just set the time zone when you
read/write from dB to get everything right.
Yes, you will have to set right time zone after connection to dB is made.
You mean to only use datetime or timestamp? It really depends on how you intend to
use the columns. But there isn't a clear have to do.
Same as above, it isn't wrong formatting your data from the dB, with a timestamp you can return date style strings from the dB so no worries
Traditionally timestamp is associated like you mention, and datetime for other dates.
more on locale/time zone:
MYSQL set timezone in PHP code

What's the best way to store date and time in a MySQL database for later display with PHP?

I want to store the date and time that a user performs an action on my website into a MySQL database. I'd like to be able to do the following with ease:
Store the date and time as one field in the database
Use a built in PHP or MySQL function to generate the date-time of the action
Store the date-time based on my server's time, and not worry about user timezones.
Order By the date-time field when I query MySQL
Later, display the date-time in many different formats using built in PHP methods
Here are my questions:
What data type should I use in MySQL ( eg. timestamp, datetime ... )?
What method should I use to generate the date-time ( eg. MySQL's now(), PHP's date() ... )?
What PHP method should I later use to format the date-time in various pretty ways ( eg. 23/4/2012, 5pm on Monday, July 2012 ... )?
I would store it as a datetime, not a timestamp.
I normally use the PHP date function and that way if you ever want to store the time relative to the user's timezone you can simply change the timezone based off the user's settings.
When you pull it out of the database, use strtotime() to convert it, then you can use all the date() features to display it however you want. Example:
echo date('F j, Y',strtotime($db_datetime)); //Displays as 'March 5, 2012'
I've struggled with this question for years, and I'm beginning to think that the best way might be to store the time as an integer that represents Unix time (number of seconds from Jan 1, 1970). I've done this and it works fine.
Personally I've never used datetime, and I can't think of a situation when I ever would use this. It just carries too many problems with it.
Timestamp is a lot better, but in MySQL it can't store a date later than 2032.
I would love to hear some serious discussion on this topic, but Stack Overflow might not be the best place for this.
If you set the mysql data type to a non-nullable timestamp, then save rows with a null value for that column, mysql will automatically update the timestamp for you.
As for reading it back out again, you can just use php's strtotime and the date object to get it into the format you need.
You should use the datetime datatype for your requirement.
It will store both the date and time from your input field based on your query.
For retrieving the datetime you can use the mysql's date_format() function or PHP's date() function.
The datetime will always be stored according to the server's time and not on the clients time.

PHP - Inserting date into datetime field

I have used a datetime field in my database for storing dates, whats the proper way to insert say todays date into that field using PHP?
Cheers,
Don't use PHP at all, but the database's built-in function for that.
Assuming you're using mySQL, the function's name is NOW().
11.7. Date and Time functions in the mySQL manual
i think you can use the php date() function
INSERT INTO table (`date`)VALUES (NOW());
Use the PHP function time() instead and store the value as a normal integer - that gives you much more possibilities.
As everyone else has said, using the MySQL function NOW() would probably be the best bet for a DATETIME column. MySQL also has many date and time functions available.
Personally, I do what Ivarska suggested and use an INT data type with PHP's time() or MySQL's UNIX_TIMESTAMP(). It gives the date in the GMT standard and it will port over easily.

Which is the better way of storing date and time in MySQL?

i would like to store the date and time in the following format in the database
a) DD-MM-YYYY or i could add up HH-MM
b) as my servers are located in US i would like to fetch the time as per IST. now that will be GMT : +5:30
earlier i used to store the date in mysql in this format timestamp(int(11)):1291746600. i used to convert the date with strtotime();
currently my intention of storing the date is just to store and display. in the future i would like to calculate the no. of days, months etc.
which would be the best possible solution for this?
P:S : i would appreciate if someone could explain me which datatype to use and how to use it with PHP.
Use DATETIME fields! They are the best format to store dates in mySQL. They offer proper indexing and optimization, and you can use the full range of mySQL's date functions.
Any specific format you need to output the fields in, you can create from a DATETIME field using DATE_FORMAT().
MySQL doesn't support time zones in DATETIME fields - you will usually set a global time zone on the server and use that.
There's good related reading on Timezones in these questions:
MySQL: keep server timezone or user timezone?
Lost in dates and timezones
Use a datetime field.
http://dev.mysql.com/doc/refman/5.1/en/datetime.html
You will have access to a lot of function for date manipulation.
If you want still to use a varchar use the ISO-TIME format (YYYY-MM-DD) not the us.
I think you should use DATETIME data type.
For more operations on date and time have a look at functions listed here
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

Categories