codeigniter - insert return true, without modifying database - php

I got this update script in a for :
if(isset($_POST[$i+1 . '_create_metier'])){
$new_entry = array(
'id_parent' => $current[0]->id,
'work' => $_POST[$i+1 . '_create_metier'],
'days' => $_POST[$i+1 . '_create_daycount'],
'price' => $_POST[$i+1 . '_create_price'],
'cr_date' => date('d-m-Y H:i:s'),
'user' => ''
);
var_dump($new_entry);
if(!$this->db->insert('meta_parent',$new_entry)){
echo 'fail';
$p3 = 0;
}else{
echo 'success';
}
}
The var_dump return a well filled array for my db structure, and 'success' is printed, implying the query worked.
But i don't get any modification in my DB. It's not my first use of codeigniter and i never had such an issue.
Thanks for help

I think the error is in the date format, MySQL accept the date in this format: Y-m-d H:i:s
Try replacing
'cr_date' => date('d-m-Y H:i:s'),
with
'cr_date' => date('Y-m-d H:i:s'),

Related

PHP Convert date Timezone to Epoch

Any help with PHP to convert the below, taking into consideration the time zone at the end, to UNIX epoch for MYSQL insert.
29/05/2022 22:23:04 +00:00
No examples of attempts :-/ Tried basic strtotime on it's own.
Just parse the date and call getTimestamp.
$unixtime = DateTime::createFromFormat('d/m/Y H:i:s P', '29/05/2022 22:23:04 +00:00')->getTimestamp();
echo $unixtime;
Output:
1653862984
In the interest of helping someone out in a pinch .. You need to do something like this. I don't know your exact timezones, if they are +0:00 or +00:00 .. But this will get you started as a stand-alone app that you can form into your own script...
<?php
$zones =array(
'-10:00' => 'America/Atka',
'-09:00' => 'America/Yakutat',
'-08:00' => 'Mexico/BajaNorte',
'-07:00' => 'Mexico/BajaSur',
'-06:00' => 'Mexico/General',
'-05:00' => 'Canada/Eastern',
'-04:-30' => 'America/Caracas',
'-04:00' => 'Chile/Continental',
'-03:-30' => 'Canada/Newfoundland',
'-03:00' => 'Brazil/East',
'-02:00' => 'Brazil/DeNoronha',
'-01:00' => 'Atlantic/Cape_Verde',
'+00:00' => 'Europe/London',
'+01:00' => 'Europe/Zurich',
'+02:00' => 'Europe/Zaporozhye',
'+03:00' => 'Indian/Mayotte',
'+03:30' => 'Asia/Tehran',
'+04:00' => 'Indian/Reunion',
'+04:30' => 'Asia/Kabul',
'+05:00' => 'Indian/Maldives',
'+05:30' => 'Asia/Kolkata',
'+05:45' => 'Asia/Katmandu',
'+06:00' => 'Indian/Chagos',
'+06:30' => 'Indian/Cocos',
'+07:00' => 'Indian/Christmas',
'+08:00' => 'Australia/West',
'+08:45' => 'Australia/Eucla',
'+09:00' => 'Asia/Yakutsk',
'+09:30' => 'Australia/Yancowinna',
'+10:00' => 'Australia/Victoria',
'+10:30' => 'Australia/Lord_Howe',
'+11:00' => 'Asia/Magadan',
'+12:00' => 'Asia/Kamchatka'
);
$my_time = '29/05/2022 22:23:04 +00:00';
$time_split = explode(' ', $my_time);
$my_date = $time_split[0];
$my_time = $time_split[1];
$my_zone = $time_split[2];
$datetime = DateTime::createFromFormat('d/m/Y G:H:s', "$my_date $my_time");
$timezone = new DateTimeZone($zones[$my_zone]);
$datetime->setTimezone($timezone);
echo "Your Date:Time: " . $datetime->format('F d, Y H:i') . "\n";
echo "Your EPOCH: " . strtotime($datetime->format('F d, Y H:i')) . "\n";

wordpress header image based on date

Im not great at PHP but I typed up the following code to change the header image based on the date array I have set.
Im wondering if the code looks alright and will function as intended or if iv missed something in my coding.
$hollidayevents = array(
array(
'image' => 'wp-content/uploads/1.png',
'start' => '01-02',
'end' => '02-02'
),
array(
'image' => 'wp-content/uploads/2.png',
'start' => '03-02',
'end' => '04-02'
)
);
foreach($hollidayevents as $myevent) {
if(date('d-m') >= $myevent['start'] && date('d-m') <= $myevent['end']) {
echo "<img src='".$myevent['image']."'>";
}
else { echo "<img src='wp-content/uploads/default header image'>";}
}
When you compare the dates as string with 'd-m' format the result is based on the day and only if days are same then it comes to compare months too.
For example imagine this: '20-10' > '01-11', both are with format you used 'd-m' what is the result ? First is higher then second so the result is true, even tho you would expect the opposite. Thats because only first character gets compared.
$hollidayevents = array(
array(
'image' => 'wp-content/uploads/1.png',
'start' => '02-01', // <- m-d format
'end' => '02-02' // <- m-d format
),
array(
'image' => 'wp-content/uploads/2.png',
'start' => '02-03', // <- m-d format
'end' => '02-04' // <- m-d format
)
);
$eventFound = false;
foreach($hollidayevents as $myevent) {
if(date('m-d') >= $myevent['start'] && date('m-d') <= $myevent['end']) {
echo "<img src='".$myevent['image']."'>";
$eventFound = true;
break;
}
}
if(!$eventFound){
echo "<img src='wp-content/uploads/default header image'>";
}

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);

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

Magento Set Grid to Filter Automatically by Current Day using Existing Datetime Column in Grid

In Magento I'm creating a custom module and would love to be able to filter automatically by the datetime column so that the intial grid listing shows only entities related to "todays" date.
Here is my datetime column:
$this->addColumn('ts', array(
'header' => $hlp->__('Activated'),
'align' => 'left',
'index' => 'ts',
'type' => 'datetime',
'width' => '160px',
));
I'm think there should be a way for me to just add a filter to the collection like so:
$now = Mage::getModel('core/date')->timestamp(time());
$dateTime = date('m/d/y h:i:s', $now);
$collection = Mage::getModel('mymodule/items')->getCollection()
->addFieldToFilter('ts', $dateTime);
But this doesn't work?
Am I using the wrong filter? My "ts" field in the database is a "datetime" field, but the default magento "From: " - "To:" date range selectors don't use hours, minutes, seconds.
Any ideas?
Thanks,
Tegan
This seems to work. I had my time formatted incorrectly.
$now = Mage::getModel('core/date')->timestamp(time());
$dateStart = date('Y-m-d' . ' 00:00:00', $now);
$dateEnd = date('Y-m-d' . ' 23:59:00', $now);
$collection = Mage::getModel('mymodule/items')->getCollection()
->addFieldToFilter('ts', array('from' => $dateStart, 'to' => $dateEnd));
I think it should be
$dateEnd = date('Y-m-d' . ' 23:59:59', $now);
instead of
$dateEnd = date('Y-m-d' . ' 23:59:00', $now);

Categories