This question already has answers here:
Deprecated: Function split() is deprecated. How to rewrite this statement?
(4 answers)
Closed 9 years ago.
My task is to split a date fetching from DB and find the date after 8 years.
My tries are here -
Variables:
$doo = $info['s_doo']; // 2013-05-01
$validity = $info['s_validity']; // 8
Try 1
$str="+".$validity." year";
echo date("d / m / Y",strtotime($str,$doo)); // Does not work
Try 2
$str="+".($validity*12)." month";
echo date("d / m / Y",strtotime($str,$doo)); // Does not work
Try 3
$str="+".($validity*52)." week";
echo date("d / m / Y",strtotime($str,$doo)); // Works but Wrong result
Finally
list($y, $m, $d) = split('-',$doo); // Line 107
$str = ($y+$validity)."-".$m."-".$d;
echo date("d / m / Y",strtotime($str)); // 01 / 05 / 2021
The output stands:
Deprecated: Function split() is deprecated in D:\****\accinfo.php on line
107 01 / 05 / 2021
If it's generating a correct output why the error message is being displayed? I don't know what the Deprecated message for.
I also tried using array instead of list and the split function like - split('-',$doo,10); split('-',$info['s_doo'],10); split('[-]',$doo); etc...
I need a good way to do the task. Thanks you.
Use DateTime instead:
// input date (Y-m-d ?)
$doo = '2013-05-01';
// 8 years ?
$validity = new \DateInterval('P8Y');
// convert input date to DateTime object and add validity
$doo = \DateTime::createFromFormat('Y-m-d', $doo);
$doo->add($validity);
print $doo->format('d/m/Y');
Deprecated means that PHP language is going to stop support for the function in future. It will be removed from up coming versions of the language and so if you have a working code now, and you upgrade your PHP in the future your code will break because it is not available in this new version. Every deprecated function gets replaced by a new better function. Find that one and replace your function with the new one.
In order to inform users, PHP will show the deprecated message even if the function in question works currently in the present PHP version.
As stated HERE split() is deprecated.
Use explode() instead like this:
list($y, $m, $d) = explode( '-' , $doo );
split() function is deprecated. You should use explode('-',$doo) which will split the string into an array.
Related
This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.
Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;
You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);
This question already has answers here:
calculate math expression from a string using eval
(10 answers)
Closed 5 years ago.
I got stucked in to achieve 45 from"0.45*100" both are in string. How can we get result as 45 from above. i have used eval() function also but no result. Please help.
Try eval() which return your operation
$test = "0.45*100";
$value=eval("return ($test);");
echo $value;
DEMO
Please check below code using eval()
Working Demo: https://eval.in/863366
$expression = '0.45*100';
eval( '$result = (' . $expression . ');' );
echo $result;
Outtput:
45
Test case scenario - User clicks on one of two links: 2012/10, or 2012/10/15.
I need to know whether the DAY is specified within the link. I am already stripping the rest of the link (except above) out of my URL, am I am passing the value to an AJAX request to change days on an archive page.
I can do this in either JS or PHP - is checking against the regex /\d{4}\/\d{2}\/\d{2}/ the only approach to seeing if the day was specified or not?
You can also do this if you always get this format: 2012/10 or 2012/10/15
if( str.split("/").length == 3 ) { }
But than there is no guaranty it will be numbers. If you want to be sure they are numbers you do need that kind of regex to match the String.
You could explode the date by the "/" delimiter, then count the items:
$str = "2012/10";
$str2 = "2012/10/5";
echo count(explode("/", $str)); // 2
echo count(explode("/", $str2)); // 3
Or, turn it into a function:
<?php
function getDateParts($date) {
$date = explode("/", $date);
$y = !empty($date[0]) ? $date[0] : date("Y");
$m = !empty($date[1]) ? $date[1] : date("m");
$d = !empty($date[2]) ? $date[2] : date("d");
return array($y, $m, $d);
}
?>
I would personally use a regex, it is a great way of testing this sort of thing. Alternatively, you can split/implode the string on /, you will have an array of 3 strings (hopefully) which you can then test. I'd probably use that technique if I was going to do work with it later.
The easiest and fastest way is to check the length of the string!
In fact, you need to distinguish between: yyyy/mm/dd (which is 10 characters long) and yyyy/mm (which is 7 characters).
if(strlen($str) > 7) {
// Contains day
}
else {
// Does not contain day
}
This will work EVEN if you do not use leading zeros!
In fact:
2013/7/6 -> 8 characters (> 7 -> success)
2013/7 -> 6 characters (< 7 -> success)
This is certainly the fastest code too, as it does not require PHP to iterate over the whole string (as using explode() does).
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is this microtime showing up weird in PHP
I use this code to check performance of the script.
$start_time = microtime(1);
// execution some code
$cpu_time = microtime(1) - $start_time;
the output of echo $cpu_time is something like 3.0994415283203E-6
how to display this to real value like 0.000000003099 on the screen ? Is it 0 seconds and very quick execution, right? :)
Right :)
Try using number_format() function:
echo number_format($cpu_time, 12);
Use number_format.
Like:
number_format($microtime, 15);
Use bcsub
Example
echo bcsub ( microtime ( 1 ), $start_time, 16 );
im trying to validate a date to see if it matchs the mysql format
this is the code
$match = "/^\d{4}-\d{2}-\d{2} [0-2][0-3]:[0-5][0-9]:[0-5][0-9]$/";
$s = $this->input->post("report_start"). " " . $this->input->post("report_start_time").":00";
$e = $this->input->post("report_end"). " " . $this->input->post("report_end_time").":59";
if($this->input->post("action") != "")
{
echo trim($s). " => " . preg_match($match, trim($s));
echo "<br>";
echo trim($e). " => " . preg_match($match, trim($e));
}
the date format goes into $s and $e are
$s = 2011-03-01 00:00:00
$e = 2011-03-01 23:59:59
and they both return false (0).
i tested the pattern on http://www.spaweditor.com/scripts/regex/index.php and it returns true (1)
http://pastebin.com/pFZSKYpj
however if i manual inter the date strings into preg_match like
preg_match($match, "2011-03-01 00:00:00")
it works.
i have no idea what im doing wrong
======================
now that i think about it, i only need to validate the houre:min part of the datetime string.
im manually adding the seconds and the date is forced by a datepicker and users cant edit it
You're making your work harder that it needs to be. In php there are many date handling functions that mean you don't have to treat dates like strings. So, rather than test that your input dates are in the correct format, just insist on the correct format:
$adate= date_create('January 6, 1983 1:30pm'); //date format that you don't want
$mysqldate= $adate->format("Y-m-d h:i:s");//date format that you do want
There are also functions to check that a date is a real date, like checkdate.
ok heres wat i did.
since im forcing the date format and the ending seconds of the time part
i just validated the hour:mini part using "/^2[0-3]|[01][0-9]:[0-5][0-9]$";
and if that returns true i put everything together end reconstructed the final datetime string
$match = "/^2[0-3]|[01][0-9]:[0-5][0-9]$/";
$s_d = $this->input->post("report_start");
$s_t = $this->input->post("report_start_time");
$e_d = $this->input->post("report_end");
$e_t = $this->input->post("report_end_time");
if($this->input->post("action") != "")
{
if(
( preg_match($match , trim($s_d." ".$s_t.":00")) )
&& ( preg_match($match , trim($e_d." ".$e_t.":59")) )
)
{
$r = $this->model_report->client_hours_logged(array($s,$e));
$data['report'] = $r;
var_dump($r);
//$this->load->view("report/client_hours_per_client",$data);
}
}
Watch out:
[0-2][0-3] is not a good regex for hour values - it will match 01, 12, 23 and others, but it will fail 04 through 09 and 14 through 19.
Better use (2[0-3]|[01][0-9]) instead.
I use this to validate a 'Y-m-d H:i:s' format date string:
match = '/^[12][0-9]{3}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[01]) ([01][0-9]|2[0-3]):[0-5][0-9]:[0-5][0-9]$/';
You could use strtotime and date to parse and format the date properly.
Why not just simply force the date into the format you want:
$e = '2011-03-01 00:00:00';
$mysqlFormat = date('Y-m-d H:i:s', strtotime($e));
Also, there is a bit of an error in your regex [0-2][0-3]:[0-5][0-9]:[0-5][0-9] will only match the hours of 00,01,02,03,10,11,12,13,20,21,22,23 so it will never match 4am, or 3pm among others. That aside I looked over your RegEx and I don't see any problems with it matching the test cases you've offered. I would check to make sure there is not extra whitespace on either side of date string with trim().
I concur with Tim : MySQL behaves in quirks mode and always tries to go easy on DATE and DATE_TIME column types. You can omit certain parts of your input and it still will try to compensate and achieve that goal successfully to some degree... That's why, most numbers your Reg-ex considers as invalid, MySQL will accept as valid.