This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Add 1 hour to datetime datatype
I have a timestamp stored in mysql db that I need to add 1 hour to before I display in our application (the timestamp is based on Central time, and I want to show Eastern). Quite simply I just need to add 1 hour to the value.
The stamp is stored as:
2012-02-12 05:20:03
Alternatively, once I retrieve it from the source, I could add an hour before storing it. I store it using the following foreach:
$created_at = strtotime($item->created_at);
Suggestions are greatly appreciated.
Instead of storing the formatted time, store the timestamp, either using the PHP time function or the mysql timestamp,
Then convert that back into a formatted time based on your needs using the php date function in this case or whatever other date formatting functions exist for other languages, except before you format the dates, add 3600 seconds/1 hr to them in order to achieve your objective.
Hope that helps.
May I suggest you use the CONVERT_TZ() function in MySQL? You just specify the original TZ and the TZ you want to convert to in the query, and voila, it's converted.
http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_convert-tz
You wouldn't need to worry about messing with the time functions in PHP.
Update tableA
set created_time= DATE_ADD(created_time, INTERVAL 1 HOUR) where unique_id =4;
Related
I'm looking for a way to get the CURRENT_TIMESTAMP returned from MySQL, and ultimately, I want to construct a DateTime($with_the_result); in PHP (CodeIgniter), and compare it with a MySQL timestamp, using the function in this accepted answer. (Of course, changing the 3rd line)
I don't want to use PHP's DateTime(); to initialize it because my PHP server (Apache?) always have the wrong values. Although MySQL could be wrong too, I just want consistency. So I want to make sure that I get 'NOW()' in PHP
How can I do that with CodeIgniter & MySQL (and maybe Active Record? not necesssarily).
I will also appreciate any comments about the difference between timestamp and datetime, and which one should I use for big databases. I just need precision up to seconds.
Thanks for any help !
Turns out I can do this :
echo $this->db->query("SELECT NOW();")->row_array()['NOW()'];
which prints the timestamp : 2014-01-25 20:22:34
Timestamp is the number of seconds passed since 1970, and date time is pretty much the date and time down to the second.
If you're trying to get the current timestamp I'd suggest using
$current_timestamp = time();
In PHP it will return the current timestamp whenever it is ran. You would then just subtract the timestamp from MySQL with the $current_timestamp;
This question already has answers here:
Should I use the datetime or timestamp data type in MySQL?
(40 answers)
Closed 8 years ago.
For my new database i want to save a given date and time and year from php to my database with mysqli, in sql i can make a field :
DATE
DATETIME
TIMESTAMP
TIME
YEAR
I'm using the database only with php, what type should i select? and for what reason.
I think datetime and timestamp are the best options. but cant find any reason why 1 should be better then the other. Can someone help me to chose ?
Or is it better to save date time and year separate?
I want to make querys to get values from last week etc.
For my new database i want to save a given date and time and year from php to my database
So, store it as DATETIME. It includes the year.
Here is how look at those types:
DATE: use for dates (with years), for example a a birthdate ("2010-11-23")
TIME: use for a time in a day, for example the start of your lunch ("12:00")
DATETIME: use for a specific date and time, for example the start of a meeting ("2010-11-23 12:00")
TIMESTAMP: use for when a specific thing happened, for example the time a certain meeting was created ("1385653500"; this often includes timezone information in its definition)
YEAR: use to store a year, for example the start year of a war ("1653")
Note that you can always cast "larger" types to "smaller" types. E.g. you can cast a DATE into a YEAR.
This question already has answers here:
Should I use the datetime or timestamp data type in MySQL?
(40 answers)
Closed 9 years ago.
I am creating a table which has create_date and modify_date field in mysql, but I am struggling in decide which data type is the best choice for these 2 fields. Should i use date time for both fields, or should i use date time for create_date and time stamp for modify_date ?
Depends on your requirements. If you want to know the exact time these things happened then use a date/time (DATETIME in MySQL). Or, if you're happy with with just the date then choose date (DATE in MySQL)! If you don't know when maybe use DATETIME just to be safe (so you have the precision if you need it).
You're using MySQL so here is the documentation: http://dev.mysql.com/doc/refman/5.0/en/datetime.html
I'm assuming you're talking about a database table. If that is the case and you need to know date information choose a date time for both modify_date and create_date. If you don't need date information use a time stamp.
Sorry I'm from Ukraine and I use Google Translate. It all depends on how you are going to use them, if you take away from each other then TimeStamp, if just to show (for example in the news), then it is better Date Time. Generally I recommend TimeStamp
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert one date format into another in PHP
I know there are plenty of posts out there, however there is not any validated or accepted answer that explains that well!
I'm working with Wordpress and use this to save a date into a database …
update_post_meta($post->ID, "event_date", $_POST["_wr_event_date"]);
I simply put in my desired date like 08.05.2012 into the _wr_event_date Field and it successfully gets stored into the database. However for further stuff I need this date to be sortable and I want to convert it into a unix timestamp.
How can I convert my entered date value "08.05.2012" (which is in german date format: %d.%m.%Y) to a unix timestamp that represents this time?
I thought of using pdate_post_meta($post->ID, "event_date", strtotime($_POST["_wr_event_date"])); but I wonder if this is the correct way because I can't think of a reason why strtotime() should automagically know what date format I've put in!?
Any thoughts on that? thank you in advance.
Just use DateTime (strtotime, works similar but I prefer DateTime):
$a = new DateTime('08.05.2012');
echo $a->getTimestamp();
strtotime and the DateTime library are very well written and they detect nearly every case, so you are good to go.
Otherwise, just pick a DATE field in your database, then you can sort it there too.
You can also use the mktime function -
mktime
This will be done with PHP.
I basically want to get the number of rows that were inserted 30 minutes ago.
I have a time field on my table which is type TIMESTAMP and on update it's set to CURRENT_TIMESTAMP.
The date is stored in this format:
2011-05-27 04:29:17
My query is supposed to look something like this, however i just can't do it
SELECT COUNT(*) FROM mytable WHERE UNIX_TIMESTAMP(time) < '.time().'-1800
Where time() is PHP's function that fetches the UNIX time.
What it should basically do is print me the number of rows inserted from now to 30 minutes ago, but i just can't seem to make it work.
Can somebody help?
Small edit:
Another problem i am seeing is that php's function time() displays the unix time which is UTC. The time stored in mysql is probably GMT i.e whatever my computer's time/timezone is set to.
You can easily get rows stored from now to 30 mins ago by simply using:
SELECT count(*) FROM mytable WHERE `time` >= DATE_SUB(UTC_TIMESTAMP, INTERVAL 30 minute)
Usage of UTC_TIMESTAMP is just an example if you're storing your date/time data as UTC_TIMESTAMP(), you can probably use NOW() if necessary, depends on what you're storing really.
**EDIT**
Removed bad pointers and fixed example :)
Do you really need your computer's timezone to be different than UTC? why not just set it to UTC & save yourself the confusion? If that doesn't work, just use dateadd() on mysql to convert your mysql timestamp to UTC when checking?
My suggestion would be to write a small function to convert the mysql timestamp to your PHP timestamp format & load it into mysql. Then all you need to do is to call tmstamp(time_stamp) instead of time_stamp in your query. You can do the reverse too i.e. Convert PHP's "30 minutes ago" timestamp to mysql format and rerun your query (probably easier).
Usually it's just a formatting issue. It's not standardized across programs.