CakePHP3, Saving current date on user registration - php

Using CakePHP3,I'm having trouble figuring out how to save the current DATE in (year, day, month) format. I'm using mySQL as the database, and I have a users table with a created_at column with type DATE. When a user registers, how do I save the current date along with all the other entity data? I've been looking around but the CakePHP Cookbook is a little unclear.

change the datatype of created_at to TIMESTAMP and set the default value to CURRENT_TIMESTAMP on the field settings.This should allow mysql to automatically set the current server date and time for you everytime a new record is created.
No code required!!!

Related

MYSQL How to keep the date data added?

I am new with MYSQL and PHP integration of it. So I am trying to make an Index where you can add websites and script shows the URL - Title - Last Updated Date. Till here I have no problem but I also want to keep the Date/Time the website added to the database. I am currently using TIMESTAMP but it changes the data everytime the websites updates.
Is there anything I can use for showing and storing the date when the data added?
Your timestamp is currently being updated on every UPDATE. I assume it is probably defined like this:
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
But you don't need the last "ON UPDATE CURRENT_TIMESTAMP"
Just this definition will automatically set the default value to the time the row is created, and it wont change.
date_added TIMESTAMP DEFAULT CURRENT_TIMESTAMP
Use a Date type on the mysql table and
when you create the record just fill the date field with php
new Datetime('now');
when you update a record do not enter anything in that field
UPDATE table SET ULR='new_url', (and all the other field but date added) Where something=something
when you retrive the date from Mysql use the format method
$date->format('Y M D hh:mm')

MySQL custom Timestamp value

I'm having some troubles dealing with Timestamp data type in MySQL.
I'm saving simple records in my database using a simple DB structure, like:
ID int
Name varchar
Date timestamp
Text varchar
And then retrieve them with something like:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-06-30'
Everything works fine if I store records letting MySQL fill the Date field with the actual timestamp, for example: 2013-10-04 22:40:02 which means I don't add any value to the Date field in my INSERT query.
But I need to be able to add the date by my self since my application needs to store the date from where the application started, and not the date and time in which the query was sent to the database.
So what I do is I create the same date/time format my Date field uses which is 2013-10-04 22:40:02 and then do a simply insert:
INSERT INTO table (Name, Date, Text)
VALUES ('Peter', '2013-10-04 22:40:02', 'Hello...')
Now, doing it this way I'm unable to bring any result by date using a select query like this one:
SELECT * FROM table WHERE Date BETWEEN '2013-01-01' AND '2013-11-30'
Even if I try to sort results by Date using PHPMyAdmin interface, all the records that contain manually added dates disappear. If I sort them by ID, they re-appear. I checked and the dates and formats are correct. So I have no idea what the problem could be. I'm new at MySQL by the way.
Hope you can give me a hand. Thanks!
Well, I think I found the problem and it has nothing to do with PHP and MySQL, the problem is that I generate the date with JavaScript, and it's giving the wrong month.. :/
Thanks to everyone anyway!

Recording time/date PHP form is inserted into SQL database?

I'm looking to order certain uploads by time order arrived (descending), but I'm not sure how I would record the time and date of submission using PHP/SQL. I'm pretty new to this, learning by coming up with projects and working through them as best as I can. Thanks for whatever help you can give me.
EDIT: I understand that the those functions exist, I just have no idea how I would implement them.
MySQL supports TIMESTAMP fields that can be automatically updated when the record is updated.
When you specify DEFAULT CURRENT_TIMESTAMP, the current time is inserted in new records. When you specify ON UPDATE CURRENT_TIMESTAMP as well, the timestamp is updated when the record is updated. That way, MySQL can automatically log the time for you.
You can do this using MySQL's NOW function.
Also see MySQL's Date and Time functions : http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html
You have to create a column for saving the date and time.
I often use the following two columns:
created DATETIME DEFAULT NOW(),
modified DATETIME DEFAULT NOW()
That's all. If you insert a new record, MySQL will automatically update created and modified to the current date and time.
And if you want to order all uploads by their creation date:
SELECT * FROM uploads ORDER BY created DESC
Also see: http://sql-info.de/mysql/examples/CREATE-TABLE-examples.html#1_5

I want to hide the details of record (of database) from user when date will be expired using php

I'm totally new to php & mysql,
When I'm creating one trial application using php ,there is problem specify below,
I want to hide the details of record (of database) from user when date will be expired using php ,
When one user logedin & create one record entry that time automatically current date is also entered in database, but I want to give 1 week expiry date, after that the record will be not shown to the front end but it available in database. When the creator of this record renew this recod that time automatically date should be updated at current date, & same record will be shown as new entry
PLz help me,
I'm waiting for ur answer,
Thanks in advance.
What kind of column is the date one? Timestamp? or timedate?
Either way, you can do it like so (depending on your column type)
For timedate:
WHERE (date + INTERVAL 1 WEEK) < CURDATE()
For timestamp:
WHERE (timestamp + 604800) < UNIX_TIMESTAMP()
Let me know what you are using for your date column and I can update. You can also see the MySQL date functions here.

How to save time as UTC time in php/mysql?

I use
date_default_timezone_set($_SESSION['time_zone']);
to set a users timezone on a page, I am wanting to know, how do I save current time to mysql in php with the current UTC date and time? Also should I be storing it as a timestamp?
My existing site used datetime fields and when I saved a new item to mysql I could just use now() in the php part making the insert or update query
Here are the relevant MySQL functions if you'd rather do it in your SQL:
SELECT UTC_TIMESTAMP(), UTC_DATE(), UTC_TIME();
UTC_TIMESTAMP() is what you're looking for if you want to store as a DATETIME field
gmdate('U'); outputs the GMT/UTC timestamp of the specified timestamp based on the current timezone.
See more information on the PHP Manual page

Categories