I tried this code but it does not work for me. i just want to add the array value to database. it give me error like this
Notice: Undefined offset: 2
Here is my code:
$timeadd = date("m-d-Y H:i:s", strtotime('+6 hours'));
$extinvoice=mysqli_query($link,"Select * from invoice WHERE BRANCH_CODE_MX='".$display_branchcode."' and INVOICE_NO_MX='".$invoicecode."' and INVOICE_ITEM_UNIT_MX='EXTENDEDWARRANTY'");
while($extrow=mysqli_fetch_array($extinvoice))
{
$ewtitemcode=$extrow["INVOICE_ITEM_CODE_MX"];
$imeiserialunit=$extrow["EWT_IMEI_MX"];
$customercode=$extrow["INVOICE_CUS_CODE_MX"];
$ewtarray[] = "('$invoicecode','$ewtitemcode', '$imeiserialunit','$customercode','$display_branchcode','$timeadd')";
}
$arrayitem=count($ewtarray);
for($item = 0; $item <= $arrayitem; $item++)
{
$sql = mysqli_query($link,"INSERT INTO extended_warranty
(INVOICE_NO_MX,FORM_EW_MX,EW_SERIAL_MX,CUSTOMER_CODE,BRANCH_CODE_MX,DATE_ADDED)
VALUES
($ewtarray[$item])");
}
The database requires a datatime in the format
2016-10-02 10:00:00
So change this
$timeadd = date("m-d-Y H:i:s", strtotime('+6 hours'));
to
$timeadd = date("Y-m-d H:i:s", strtotime('+6 hours'));
Additionally to the other users I have removed the double brackets. What happens now?
$timeadd = date("Y-m-d H:i:s", strtotime('+6 hours'));
$extinvoice=mysqli_query($link,"Select * from invoice WHERE BRANCH_CODE_MX='".$display_branchcode."' and INVOICE_NO_MX='".$invoicecode."' and INVOICE_ITEM_UNIT_MX='EXTENDEDWARRANTY'");
while ($extrow=mysqli_fetch_array($extinvoice)) {
$ewtitemcode=$extrow["INVOICE_ITEM_CODE_MX"];
$imeiserialunit=$extrow["EWT_IMEI_MX"];
$customercode=$extrow["INVOICE_CUS_CODE_MX"];
$ewtarray[] = "('$invoicecode','$ewtitemcode', '$imeiserialunit','$customercode','$display_branchcode','$timeadd')";
}
for ($item = 0; $item < count($ewtarray); $item++) {
$sql = mysqli_query($link,"INSERT INTO extended_warranty
(INVOICE_NO_MX,FORM_EW_MX,EW_SERIAL_MX,CUSTOMER_CODE,BRANCH_CODE_MX,DATE_ADDED)
VALUES
$ewtarray[$item]");
}
Related
I have a PHP script which records things based on the day. So it will have a weekly set of inputs you would enter.
I get the data correctly, but when i do $day ++; it will increment the day, going passed the end of the month without ticking the month.
example:
//12/29
//12/30
//12/31
//12/32
//12/33
Where it should look like
//12/29
//12/30
//12/31
//01/01
//01/02
My script is as follows:
$week = date ("Y-m-d", strtotime("last sunday"));
$day = $week;
$run = array(7); //this is actually defined in the data posted to the script, which is pretty much just getting the value of the array index for the query string.
foreach( $run as $key=>$value)
{
$num = $key + 1;
$items[] = "($num, $user, $value, 'run', '$day')";
echo "".$day;
$day ++;
}
Should I be manipulating the datetime differently for day incrementations?
You can use
$day = date("Y-m-d", strtotime($day . " +1 day"));
instead of
$day++;
See live demo in ideone
You refer to $day as a "datetime" but it is just a string - that is what date() returns. So when you do $day++ you are adding 1 to "2015-12-02". PHP will do everything it can to make "2015-12-02" into a number and then add 1 to it, which is not date math. Here is a simple example:
<?php
$name = "Fallenreaper1";
$name++;
echo $name
?>
This will output:
Fallenreaper2
This is how I would do it, using an appropriate data type (DateTime):
<?php
$day = new DateTime('last sunday');
$run = array(7);
foreach ($run as $key => $value) {
$num = $key + 1;
$dayStr = $day->format('Y-m-d');
$items[] = "($num, $user, $value, 'run', '$dayStr')";
echo $dayStr;
$day->modify('+1 day');
}
To increase time you should use strtotime("+1 day");
here is simple example of using it
<?php
$now_time = time();
for($i=1;$i<8;$i++) {
$now_time = strtotime("+1 day", $now_time);
echo date("Y-m-d", $now_time) . "<br>";
}
?>
i'm trying to add X month to a date taken from my database
$sql_batch = "SELECT * FROM mgm_subscription WHERE status = '1'"
$query_batch = mysql_query($sql_batch);
$row_batch = mysql_fetch_array($query_batch);
$addMonth = 3;
$startDate = $row_batch['start_month'];
$endDate = strtotime('+'.$addMonth.' month', $startMonth); // add number of days from form
$endDate = date('m/d/y H:i:s', $endDate );
$sql_date = "INSERT INTO user_subscription (user_id, start_month, end_month, sub_status) VALUES ('".$usercode2."','".$startDate."','".$endDate."', '')";
$query_date = mysql_query($sql_date);
NULL was inserted into the end_month.
start_month and end_month is DATE type in the mysql
how do i fix this? tq.
If I understood your question, your $endDate should be
$endDate = date('Y-m-d', strtotime($startDate.' +'.$addMonth.' months'));
So this will equate to:
$endDate = $startDate + 3 months /* in Y-m-d format */
EDIT: Just saw that your column datatype is Date. This would mean that you can't store timestamp in your date. It has to be Y-m-d format only as that is the valid mysql format supported.
You insert $endMonth a value(number of days from form) then you try to replace it ($endMonth) and you call back your replaced variable..
$endMonth = strtotime('+'.$addMonth.' month', $startMonth); // add number of days from form
$endMonth = date('m/d/y H:i:s', $endMonth);
It will return null value.. My suggestion, try to put other variable to prevent duplicate value or missing data
You only mention adding X months. Maybe I misunderstood your question, but if all you care about is the month, I would do the following:
if ($startMonth === 'January') {
$monthNumber = 1;
} else if ($startMonth === 'February') {
$monthNumber = 2;
} //Up to November then finish with {
else {
$monthNumber = 12;//December
}
$newMonthNumber = $monthNumber + $addMonth;
if ($newMonthNumber % 12 == 1) {
$endMonth = 'January';
} else if ($newMonthNumber % 12 == 1) {
$endMonth = 'February';
} //Up to November then finish with {
else {
$endMonth = 'December';
}
$sql_date = "INSERT INTO user_subscription (user_id, start_month, end_month, sub_status) VALUES ('".$usercode2."','".$startMonth."','".$endMonth."', '')";
$query_date = mysql_query($sql_date);
I'm trying to take individual values for date/time from a form submission and parse them into a date of a certain format.
If validation fails for any reason (e.g. 30th Feb, 13th month, etc) then the result should default to the current time.
if (isset($_POST['year'], $_POST['month'], $_POST['day'], $_POST['hour'], $_POST['minute']))
{
$y = $_POST['year'];
$m = $_POST['month'];
$d = $_POST['day'];
$h = $_POST['hour'];
$i = $_POST['minute'];
if (checkdate($m, $d, $y))
{
if ($h >= 0 && $h <= 23)
{
if ($i >= 0 && $i <= 59)
{
$str = $y.'-'.$m.'-'.$d.' '.$h.':'.$i.':00';
$time = strtotime($str);
}
else $time = time();
}
else $time = time();
}
else $time = time();
}
else $time = time();
$datetime = date('D j M Y - H:i:s T', $time);
echo $datetime;
Two things happen which I don't understand:
If one or more date values are missing, an error appears about checkdate()... I can't see why the validation doesn't just fail at the first if()
If one or more time values are missing, the end result is the UNIX Epoch?!
If one or more date values are missing, an error appears about checkdate()... I can't see why the validation doesn't just fail at the first if()
Being set and having a valid value are two different things. A variable can be set and contain an empty string or null. Check to make sure those values actually contain values using empty().
If one or more time values are missing, the end result is the UNIX Epoch?!
If you pass date() an invalid second parameter (i.e. a valid unix timestamp) it defaults to the epoch.
Empty value check before execute checkdata
<?php
if(!empty($y) && !empty($m) && !empty($d) && !empty($h) && !empty($i))
{
if (checkdate($m, $d, $y))
{
if ($h >= 0 && $h <= 23)
{
if ($i >= 0 && $i <= 59)
{
$str = $y.'-'.$m.'-'.$d.' '.$h.':'.$i.':00';
$time = strtotime($str);
}
else $time = time();
}
else $time = time();
}
else $time = time();
}
else $time = time();
$datetime = date('D j M Y - H:i:s T', $time);
echo $datetime;
?>
i need to compare two dates where if one date is greater than the other then an sql will run. this is my code
date_default_timezone_set('Asia/Kuala_Lumpur');
$date = date('Y-m-d G:i:s');
$query = mysql_query("SELECT * FROM package_transaction");
if(mysql_num_rows($query) > 0) {
while($row = mysql_fetch_array($query)) {
$transac_code = $row['transac_code'];
$duedate = $row['payment_due'];
if(strtotime($date) > strtotime($duedate))
{
mysql_query("UPDATE package_transaction SET `status` = 'cancelled' WHERE `payment_due` = `$duedate` AND `transac_code` = `$transac_code`");
}
}
}
but its not working. please help
try this,
date("Y-m-d", strtotime($date)) > date("Y-m-d", strtotime($duedate))
could you try and use the date format before using strtotime
$duedate = $row['payment_due'];
$duedate = $duedate->format('Y-m-d G:i:s');
I have the day and the month from the database, stored in seperate variables. But if today is the 7th and I minus one week, it would be 0 same case for the month. And I want to add the lo_date to lockin_period_date and minus 3 months, as I want it to send an email to me 3 months in advance. How do I set the values I have from the database to the date I want? Please help.
<?php
include"connect_mysql.php";
$reminder_dates = mysql_query("SELECT*FROM registration_form");
while($row = mysql_fetch_array($reminder_dates)){
$main_dob_day = $row['main_dob_day'];
$main_dob_month = $row['main_dob_month'];
$main_dob_year = $row['main_dob_year'];
$joint_dob_day = $row['joint_dob_day'];
$joint_dob_month = $row['joint_dob_month'];
$joint_dob_year = $row['joint_dob_year'];
$loan_lo_day = $row['loan_lo_day'];
$loan_lo_month = $row['loan_lo_month'];
$loan_lo_year = $row['loan_lo_year'];
$lockin_period_day = $row['lockin_period_day'];
$lockin_period_month = $row['lockin_period_month'];
$lockin_period_year = $row['lockin_period_year'];
$legal_fee_clawback_day = $row['legal_fee_clawback_day'];
$legal_fee_clawback_month = $row['legal_fee_clawback_month'];
$legal_fee_clawback_year = $row['legal_fee_clawback_year'];
date_default_timezone_set('UTC');
$m = date("n");
$d = date("j");
$y = date("Y");
$time = time();
$today = date('n-j-Y', $time);
//main applicant birthday - reminder 7days in advance
$main_day = $main_dob_day-7;
if($main_day == $d && $main_dob_month == $m){
echo "Mail Sent!";
}
//
}
?>
A working solution would be:
$main_day = date( 'n-j-Y', strtotime( "today -1 week"));
To apply it to your use-case:
$time = mktime( 0, 0, 0, $main_dob_month, $main_dob_day, $main_dob_year);
$main_day = date( 'n-j-Y', strtotime( "-1 week", $time)); echo $main_day;
$oneWeekAdv=$main_dob_year.'-'.$main_dob_month.'-'.$main_dob_day;
$oneWeekAdv=strtotime(date("y-m-d", strtotime($oneWeekAdv)) . " -1 week");
$oneWeekAdv=date('m-d', $oneWeekAdv);