insert current datetime to custom wordpress datetime column - php

I have an issue with inserting current datetime to custom wordpress datetime column. I've used curent_time function I also used date('Y-m-d H:m:s') as value for the column but it did not work.
I will provide my code for any suggestions:
global $wpdb;
$load_rec_table = $wpdb->prefix.'load_rec';
// Insert into load record table
$wpdb->insert($load_rec_table,array(
'rec_date' => current_time('mysql'),
'total_load' => $_POST['totalPower'],
'total_gap' => $_POST['totalGap'],
'carriage' => $_POST['carriage']
));

You can use NOW() or CURRENT_TIMESTAMP() functions of Mysql
global $wpdb;
$load_rec_table = $wpdb->prefix.'load_rec';
// Insert into load record table
$wpdb->insert($load_rec_table,array(
'rec_date' => NOW(),
'total_load' => $_POST['totalPower'],
'total_gap' => $_POST['totalGap'],
'carriage' => $_POST['carriage']
));

Related

CakePHP: How to change default value of Date input

I am trying to make a date input that defaults to the value that is already in the database.
However, when I set the month, year, and date to the database values, the month, date, and year attributes are set on the select element but the page still displays the current date as the default values. When the form is submitted, today's date is stored in the database.
Heres the code:
$mail_date_time = \explode(" ",$campaign["MailedDate"]);
$mail_date = explode("-",$mail_date_time[0]);
echo $this->Form->create("Campaign");
echo $this->Form->input("MailedDate",array(
'month' =>strtotime($mail_date[1]),
'year' => strtotime($mail_date[0]),
'day' => strtotime($mail_date[2])
));
echo $this->Form->end("Submit");
$mailed_date turns out to be: [0]=2009 [1]=11 [2]=11
Does anyone know how to solve this?
Thanks!
The Cake form helper includes formatting of dates... i.e. 'dateFormat' => 'DMY' in your MailedDate input.
The value displayed by the input field would default to the value contained in $this->data and can be overwritten completely using 'default' => 'value', or prefilled using 'empty' => 'value'.
In order to precompile the datetime fields is necessary to use the 'default' keyword:
$mydate = '2015-09-10 06:40:00'
echo $this->Form->input('datetime', array(
'label' => 'Date 2',
'default' => $mydate
));

Building an Array with a NOW() command for datetime

OK I have a mySQL table that has a few date columns in, it is set up so that there is a create date and a update date. The UpdateTimeStamp column updates automatically using the on update CURRENT_TIMESTAMP Attributes,
Its my understanding that you can only use a one timestamp in this special way, and this is fine.
So on the createdate column I need to manually add the date that the record was created its a datetime column.
I have read that you can use the NOW() command for this. However using it in the following brings me a undefined function error. I have tried it as just 'CreateDate' => NOW(), which gives the same error or using 'CreateDate' => set_value('NOW()'), does not produce an error but no value is entered into the database for this column.
form_data = array(
'ClientID' => set_value('ClientID'),
'ClientReference' => set_value('ClientReference'),
'CreateDate' => set_value(NOW()),
'Gender' => set_value('Gender'),
'MaritalStatus' => set_value('MaritalStatus'),
'ContactNumber' => set_value('ContactNumber'),
'Nationality' => set_value('Nationality'),
'Ethnicity' => set_value('Ethnicity'),
'ClientNotes' => set_value('ClientNotes')
);
I Have tried all the answers you have provided and its still not working its letting me add to the database but the CreateDate value is 0000-00-00 00-00-00
If your mysql column type DATETIME:
'CreateDate' => set_value(date('Y-m-d H:i:s'))
if TIMESTAMP:
'CreateDate' => set_value(time())
You can have both set automatically on insert and update.
Use a default value of CURRENT_TIMESTAMP on "CreateDate" so it sets automatically when you create the record, and a trigger to update "UpdateTimeStamp" when the row is altered
CREATE TABLE Clients (
ClientID INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
/* Other Columns */
CreateDate TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
UpdateTimeStamp TIMESTAMP NULL
) ENGINE=InnoDB;
CREATE TRIGGER ClientsUpdate
BEFORE UPDATE ON Clients FOR EACH ROW
SET NEW.UpdateTimeStamp = NOW();
There is no now() function in PHP, what you are looking for is the time() function. However, this will return a timestamp such as 1370183405.
MySql has a now() function in which case you need to wrap it in quotes:-
'CreateDate' => set_value('NOW()'),
Or in PHP you could do:-
'CreateDate' => set_value((new DateTime())->format('Y-m-d H:i:s')),
Nearly right,
I tried it without the set_value in so
'CreateDate' => set_value(date('Y-m-d H:i:s')),
And that worked. dont know what difference it makes

Insert Date into database table using Zend framework insert method

I have the foll code as:
$table_project_win = new Application_Model_DbTable_AfterWinProject();
$data_win = array(
'project_id' => $project_id,
'project_name' => $project,
'project_type_id' => $pro_type,
'start_date' => $dateStart,
'end_date' => $dateEnd,
'project_size' => $size,
'project_description' => $pro_des
);
$table_project_win->insert($data_win);
Here I get the $dateStart and $dateEnd variabled using as:
$dateStartt = $this->_getParam('dateStart');
echo 'date Start: '.$dateStartt;
$dateStart='"'.$dateStartt.'"';
$dateEndd = $this->_getParam('dateEnd');
$dateEnd='"'.$dateEndd.'"'
By using getParam I get the value of the date that the user has given input But when i will insert it into the database I use as
$dateStart='"'.$dateStartt.'"';
$dateEnd='"'.$dateEndd.'"'
But in the database table the value for date inserted is '0000-00-00' While when I echo the $dateStart which I have got through getParam It gives the correct value as '2012-12-11'.What is the reason of it??What Should I do??
replace $dateStart='"'.$dateStartt.'"';
with
$dateStart= $dateStartt ;
or
$dateStart='`'.$dateStartt.'`';

PHP date() function inserting zeros into database

I have an array like so which has the column names of the table on the left and the associating values on the right.
$proceduredata = array
(
'patient_id' => $patientfk,
'name_id' => $procedurenamefk,
'department_id' => $departmentfk,
'dosage_id' => $dosagefk,
'edocument' => NULL, //not implemented yet
'user_id' => $this->session->userdata('userID'),
'duration' => NULL, //not implemented yet
'submitted' => date('d-m-Y H:i:s', now()),
'comment' => NULL, //to be implemented
);
This array is then passed into a SQL insert function. The insert works fine but my "Submitted" column is getting values of this only:
0000-00-00 00:00:00
I made sure the time formats are matching? Is there something I have missed thanks.
change the date format
submitted' => date('Y-m-d H:i:s', now())
I believe that the "now()" function is a sql function and you are using PHP with the "date()" funciton. Try changing "now()" to "time()" which will give you a proper unix timestamp that the "date()" function can use to create a properly formatted date.
EDIT: I just realized I am not familiar with codeignighter, so please excuse me if the "now()" function is part of that framework.
now() is not a function, change now() to time(). Alternatively, date()'s second paramater is optional if you omit it date automatically uses the current date/time:
echo date('d-m-Y H:i:s');
will echo the current date/time in the format requested.

Passing MySQL's CURRENT_TIMESTAMP to Zend_DB update statement

How do I pass mysql's CURRENT_TIMESTAMP when using Zend_DB's update statement? The following doesnt seem to be working.
I have something like this:
$update = array(
'Name' => 'John',
'DT_Modified' => 'CURRENT_TIMESTAMP'
);
$db->update('usertable', $update );
to run a query that is represented like this:
UPDATE usertable SET Name='John', DT_Modified = CURRENT_TIMESTAMP
Try using Zend_Db_Expr to avoid unnecessary quoting:
$update = array(
'Name' => 'John',
'DT_Modified' => new Zend_Db_Expr('CURRENT_TIMESTAMP')
);
$db->update('usertable', $update );

Categories