Cant update Timestamp - php

I am having problem with my MySQL database.
I have 4 column id, user, msg, time.
Type of time is "timestamp",
Attributes is " ON UPDATE current_time_stamp"
but it always storing 0000-00-00 00:00.
I want to store it current date and time please help

I really am not able to understand what is wrong in it. As an alternative, you try to get the current time using date() function of php and store it in your database table.
$dt=date('h:i:s:a')
Will give you the current time in hour:minute:second:am/pm format.
To get date also, you can use:
$dt=date('y-m-d h:i:s:a')
Hope it will help you.

Related

How to set the current time as static variable using PHP?

I need to set the current date and time in static variable.
I need to insert the 50 records into database table. Here,I need to insert the current date and time. Then, I need to set the current date and time of the 50 records are same. I used this date('Y-m-d H:i:s'); format. This format will change every minutes and seconds.
How to I do. Please help me.
$date = date('Y-m-d H:i:s')
then use this variable,for create records. All records will have same time.
It's been a long time, but I found a simple solution. Maybe it's useful for someone (like me) that wants to do the same thing nowadays.
Even when you store the current time/datetime to a variable, the time it's still running, so it changes every second.
I solved it by storing the time() value into a MySQL table (datetime type of course), so the captured time will be stored as it is and stop running and changing every second. Then, when I want to use it, I just make a query from the MySQL table.
It's a simple way (for me) to capture the "now" value and make it stop running, but maybe there's a better way.

PHP MySql: how save timestamp UTC, like "Flickr"?

For default i'm using this snippet in my codes
setlocale(LC_TIME,'it_IT');
and I did save all my dates in mysql in a timestamp format.
In a view of make international one of my codes, i would like to save in MySQL a similar value
2013-07-12T07:59:27+0000
but of course with the +2 hours. So a Polish user will have a +3 and so on...
What's the best field for mysql to store that value? I need also to work with date, from PHP and/or from directly MySQL (for example)
SELECT id WHERE data BEETWEN [...]
Of course if I start with correct way, i don't need to change in future all my dbs, codes, etc...
Thank you very much!
For me is better store date time as UNIXTIME, it avoid the time difference between user from different locations.
You can use:
SELECT * FROM table_name
WHERE field_name
BETWEEN UNIX_TIMESTAMP('2013-07-12 07:59:27')
AND UNIX_TIMESTAMP('2013-07-13 07:59:27')
to get the Date Range

Date being changed when updating MySQL field

I'm running into a weird issue where I'm posting a date in Y-m-d format yet it's being changed to a completely different date once I view in the actual MySQL table.
Here's the query
UPDATE $admins_table
SET expire=$expireu
WHERE identity='$donation_row[steam_id]
The expire field is what I'm having issues with. The field itself is a varchar, and the $expireu variable is always a date in Y-m-d format ex. 2013-11-16
When that query is run, with the date I gave as an example above, I get a weird result in the actual MySQL table. If I go to view the table, instead of it storing 2013-11-16 it has stored 1986 as the date. No month or day, just 1986.
I may have made a very stupid/silly mistake, but at this point I'm unsure of what I've flubbed. Any help in the right direction would be much appreciated, thank you.
haha, use quotes!
UPDATE $admins_table SET expire='$expireu' WHERE identity='$donation_row[steam_id]'
mysql substracts 2013-11-16 == 1986
the use of ' and " are your friends. you are passing a math problem into mysql which it is solving and then saving the result of. wrap that date in quotes.

php mysql - phpmyadmin DATE

i'm new to databases. I have just created my first database, so far so good. Now I looking to add time in to this.
I have certain table, when it's displayed I need it to show the table info + the date that info was added. Also possible the difference in between (5day 5hours ago) or something of the sort.
Do i need to add another column to the data table where the time will be stored? If so is there a way to auto increment that to current date? How do I go about doing that?
Not really sure where to start can someone please point me in the right direction perhaps a tutorial somewhere? Cant seem to find anything solid on this topic
Thanks a bunch!:)
For each record in your DB, you want to know when it has been created?
You have to add a column of type "timestamp". You can then set the default to "current_timestamp".
Note also that you can use the "on update current_timestamp". Instead of saving current time on insertion, it will do so each time your record is updated.
If you need the 2 dates (created_at and updated_at), you'll need 2 columns. EDIT : well you can't :p
There is a datetime as well as a timestamp field. If you use a timestamp field you can have the field automatically populate with the timestamp of when the row was inserted.
http://dev.mysql.com/doc/refman/5.0/en/datetime.html
http://dev.mysql.com/doc/refman/5.0/en/timestamp-initialization.html
I suggest trying out http://sqlzoo.net/ for a gentle introduction to SQL.

Adding values to a Timestamp

I am trying to create a script for quiz. I have stored current timestamp in MySQL DB as quiz start time, and want to check after each quiz how much time is left.
I have the idea that I will add 30 mins to saved time stamp and subtract this value from current time. That will be the time left. But I don't know the exact way of doing this.
My time stamp is saved in DB in format 2010-08-24 20:08:59. Any one have the idea.
Please let me know if someone have done it, or know how to get it.
Adding 30 mins to time stamp and showing the user how much time is left.
I am using the now() function to store the timestamp in DB.
Thanks
I would personally store the output of PHP time() in the database.
If you a human readable format from this value, you could use date('Y-m-d H:i:s', $fromdatabase);.
You want to store an actual UNIX timestamp in the database, not a string in that format.
You may or may not be doing this already, it depends on the type of column you're using. For MySQL, you should be using TIMESTAMP, which allows you to retrieve the timestamp with
SELECT UNIX_TIMESTAMP(column_name) ...
To store the current time + 30 minutes, all you have to do is:
INSERT INTO table (column_name) VALUES(UNIX_TIMESTAMP() + 1800)
You can know if the time has expired by comparing time() against the value of the column.

Categories