Insert Date into database table using Zend framework insert method - php

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.'`';

Related

Eloquent Model saved to DB with day and month inverted

I am trying to save my Laravel Eloquent model to the database.
All date properties are saved in wrong format to DB (even created_at and updated_at).
In example below, my updated_at has been renamed as S_MODDATE but it's still the "native" field.
Reading the date fields works fine.
Example
My date in PHP : 2021-12-07 (7th of December)
When saved to database : 2012-07-12 (12th of July)
When read again from database : 2012-07-12
Minimal example of my code :
$currentDatetime = new \DateTime();
Log::debug($currentDatetime->format("Y-m-d h:i:s")); // Looks correct
$approRecord = T_Appro::find($approvalId);
$approRecord->DATESTATUT = $currentDatetime;
DB::enableQueryLog();
$approRecord->save();
Log::debug(DB::getQueryLog());
Corresponding log
[2021-12-07 13:17:25] local.DEBUG: 2021-12-07 01:17:25
[2021-12-07 13:17:28] local.DEBUG: array (
0 =>
array (
'query' => 'update [t_appro] set [DATESTATUT] = ?, [t_appro].[S_MODDATE] = ? where [T_APPROID] = ?',
'bindings' =>
array (
0 =>
DateTime::__set_state(array(
'date' => '2021-12-07 13:17:25.981319',
'timezone_type' => 3,
'timezone' => 'Europe/Brussels',
)),
1 => '2021-12-07 13:17:28.035',
2 => 'S000000029',
),
'time' => 468.81,
),
)
Tested solutions
Add protected $dateFormat = 'Y-m-d H:i:s'; to my model
Add following code to my model
public function getDateFormat()
{
return 'Y-m-d H:i:s.v';
}
Set app.php timezone to Europe/Brussels

updateOrCreate does not match Carbon date

I am using Laravel Framework 6.16.0 and I am creating from a string a date object so that I can input it in my db:
$transaction_date = Carbon::createFromFormat('Y-m-d', $tradeDate);
$filling_Date = Carbon::createFromFormat('Y-m-d H:i:s', $fillingDate, 'UTC');
$product = Product::updateOrCreate(
[
'price' => trim($price),
'qty' => $qty,
'companies_id' => $company->id,
'persons_id' => $person->id,
'amount_range' => $owned,
'filling_date' => $filling_Date,
'transaction_date' => $transaction_date,
],
[]
);
When running the above query my product does not get found as $filling_Date and $transaction_date are not matched in the database, even if my product already exists.
I am guessing, the reason is that I am creating a "new" Carbon object.
Any suggestions how to match the filling_date and transaction_date in the database?
I appreciate your replies!
Try this when converting a string to a date with Carbon
$date = Carbon::parse($yourStringDate);

Retrieve data based on year and month in cakephp

I am trying to create a get statements function and I have some problems defining the date range.
The data will be passed via get method in the following format: ?year=yyyy&month=mm
The relevant column of the table has datetime type (2012-02-01).
I checked the TimeHelper class and more particularly the TimeHelper::daysAsSql($begin, $end, $fieldName, $timezone = NULL) but i am not sure if it applies for this case.
I was thinking if i can create a date in the format of Y-m using the two variables year and month and then use it in the conditions for retrieving the data but I am sure there is a more efficient and proper way. Could anyone help?
$user_id = $Client['Client']['id'];
$year = $this->request['url']['year'];
$month = $this->request['url']['month'];
//$date_created = date('Y-m');
if (!empty($user_id) && (!empty($date_created))) {
$Reports = $this->Report->find('all', array(
'fields' => array('amount','currency','date_created','invoice_no'),
'conditions' => array(
'Report.client_id'=> $user_id,
'Report.date_created >=' => $date_created,
'Report.date_created <=' => $date_created
),
)
);
MONTH(dateColumn) Returns an integer that represents the month of the specified date.
YEAR(dateColumn) Returns an integer that represents the year of the specified date.
InCakephp use as
$user_id = $Client['Client']['id'];
$year = $this->request['url']['year'];
$month = $this->request['url']['month'];
//$date_created = date('Y-m');
if (!empty($user_id) && (!empty($date_created))) {
$Reports = $this->Report->find('all', array(
'fields' => array('amount','currency','date_created','invoice_no'),
'conditions' => array(
'Report.client_id '=> $user_id,
'MONTH(date_created ) >='=> $month,
'YEAR(date_created ) <=' => $year
),
)
);

Yii - find all by composite key

How do I find all by primary key? I don't want to specify the start and end dates as they aren't relevant when opening within booking form
$bookingRoom = BookingRoom::model()->findByPk(array('roomId' => 1, 'bookingId' => 1, 'startDate' => '20140619', 'endDate' => '20140620'));
You should use the findAllByAttributes() option:
$bookingRoom = BookingRoom::model()->findAllByAttributes(array('roomId' => 1, 'bookingId' => 1, 'startDate' => '20140619', 'endDate' => '20140620'));
If you don't want the startDate and endDate included remove it from the array.
Here is the documentation:
http://www.yiiframework.com/doc/api/1.1/CActiveRecord#findAllByAttributes-detail
you can try this:
$id = // code to set the id
$bookingRoom = BookingRoom::model()->findAllByPk($id);

String datetime insert into mysql database with codeigniter

I am having following MySQL table
EventId ObjectKey Title Description When Duration Where Status
INT CHAR(36) VARCHAR(500) VARCHAR(8000) DATETIME VARCHAR(500) VARCHAR(500) TINYINT
and my PHP array is
$data = array(
'Title' => $title,
'Description' => $description,
'When' => $when,
'Duration' => $duration,
'Where' => $where
);
The variable $when contains 02/21/2013. When I try to insert to the table with CodeIgniter
public function insert_event($guid, $data)
{
$CI = & get_instance();
$CI->load->database('default');
$CI->db->set($data);
$CI->db->set('ObjectKey', $guid);
$CI->db->set('Status', 1);
$vari = $CI->db->insert('Events');
return $vari;
}
Everything inserted correctly except date. Can you help me on this?
Use the correct MYSQL format for the date YYYY-MM-DD. For example change this in your code:
$data = array(
'Title' => $title,
'Description' => $description,
'When' => date('Y-m-d', strtotime($when)),
'Duration' => $duration,
'Where' => $where
);
Dates in mySQL are YYYY-MM-DD
You are inserting a date MM/DD/YYYY
So try this:
$data = array(
'Title' => $title,
'Description' => $description,
'When' => date('Y-m-d', strtotime($when)),
'Duration' => $duration,
'Where' => $where
);
Simplest way to store string date in any format in mysql
$srcFormat = "m/d/Y"; //convert string to php date
$destFormat = "Y-m-d" //convert php date to mysql date string
$data = array(
'Title' => $title,
'Description' => $description,
'When' => DateTime::createFromFormat($srcFormat, $when)->format($destFormat),
'Duration' => $duration,
'Where' => $where
);

Categories