PHP - Inserting date into datetime field - php

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.

Related

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.

MYSQL DateTime vs strtotime string held in database

I am trying to work out which is the best solution for me - i am making a roster application and have been working with the string, but think it may be beneficial to switch to the datetime type - currently working with dates is getting hard, trying to put the shifts onto a roster without doing a query for every single cell.
If you have something that's a date or a date/time then it's always better to use the right datatype for that. You get many benefits in terms of being able to do functions on it, having comparisons work, etc. It will also map well to the date/time types in your preferred language.
I think storing the dates as a datetime field makes the most sense, but I agree, it's not the easiest to deal with. I have a function in my date library that turns MySQL datetime values into a PHP timestamp.
function datetime_to_timestamp($datetime)
{
$timestamp = strtotime( $datetime );
return $timestamp;
}
Hope that helps.
PHP function strtotime($datetime) converts mysql datetime fields to unix timestamp and works really well. Why using an extra wrapper?

Insert current time with NOW() or TIME()

Which of the method is better to insert current time in mySQL.
PHP: time() in db column type INT(10)
or
MySQL: Now() in db type (datetime)
Thanks.
The second one is generally better as you don't need to convert it to a proper datetime before using other MySQL datetime functions on it.
storing a datetime is more useful for things like finding how many days have past. you can use all of date/time mysql functions in your queries.
The second - Now in datetime. Time returns a string. You'd need datetime most likely for different time-based calculations, plus there are tons of way to retrieve and format it with your queries.

Any better way to save dates in db than time()?

I am used to save dates in db as INT(11) with a time().
Considering the limitation of time() are there any better way to save that?
I would like to NOT use the database own DATE type (and all the db own date functions).
Thanks
Ok, from the comments, I understand that the problem with using time() is that we're looking to represent dates outside the 01/01/1970 to whenever/2038 range.
In this case, I think it's best to format dates for the DB as YmdHis, stored in a BIGINT (or just Ymd in INT if time isn't needed). You can get use date_create("now")->format($fmt) instead of time(), and where $fmt is either 'Ymd' for date-only or 'YmdHis' for date+time
This gives a latest date somewhere in 922,337,203AD and an earliest in -922,337,203BC with time, or 214,748AD to -214,748BC in an INT with no time.
Use $_SERVER['REQUEST_TIME'].
It's constant for the whole request and it's faster than time() (and UNIX_TIMESTAMP()) because it only requires an array lookup instead of a function call.
It is strange to avoid the standard time managing in DB. Have you ever considered all possible ways of representing if the correctly formated date field?
MySQL::Date and time functions
PostgreSQL::functions datetime
Storing the date in the correct format is more flexible and more efficient in some cases.
Datetimes are more readable for debugging and reading but the same amount of effort as timestamps for date formatting, the NOW keyword in the query makes things clean and tidy too, especially if you don't need the variable apart form the query:
INSERT INTO `mytable` (`id`,`title`,`created`) VALUES (NULL, 'my awesome record', NOW());
You could just use the built in database types for dates and times, or you could just make three integer columns in your table and save the date as integers. Whatever works and is easy to deal with.

How would I do the mysql timestamps in 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.

Categories