i am trying to get the users age, i am getting the corrrect output but with a warning in my html page
Error:
A PHP Error was encountered
Severity: Notice
Message: A non well formed numeric value encountered
Filename: controllers/users.php
Line Number: 45
Code i used is :
$now = time();
$data['dob'] = strftime('%Y', $now)-strftime('%Y',$query['dob']); //it should be showig user age
what could be the reason for the above shown error, Please help
strftime returns a string, you must convert it to number before subtraction.
EDIT: seeing the comment below, you cannot subtract, as dob is not in the same format returned by time(). So, proceed as in the paragraph above.
EDIT2: cleanest way to subtract two dates is DateTime
i converted the string into int , and subtracted the value to get my output
$userdob=(int)$query['dob'];
$data['dob'] = strftime('%Y', $now)- strftime('%Y',$userdob);
It is probably because $query['dob'] contains something other than an integer.
strftime() expects it's second argument to be a timestamp (that is, a unix-timestamp, an integer representing seconds past the epoch). If I had to guess, I'd guess that you're $query['dob'] contains some kind of string.
Related
I need to set variable, let's call it $times to specific amount of 8AM. I tried using mktime(08,00) but it returns
Parse error: Invalid numeric literal
Since I'm new to php, I still don't know which function is best used for thing such as this, weather it is time() date() or so.
My question is: how do I set $times to be 8AM of current day?
I've checked a lot of similar questions, but none of them have an answer. Not even the one that this is marked duplicate of.
Just remove the leading zeros.
mktime(8, 0);
It's because PHP is interpreting 08 as an octal number, and 8 is out of range in octal (0-7).
You can use date()
Dateformat reference: https://www.php.net/manual/en/datetime.format.php
$time = date("H:i") // 08:00
I am trying find the next date.Suppose today is 23-07-2019 and i want to echo 24-07-2019 but i can't. I execute the below code and it gives me some error.
they give this error :
Notice: A non well formed numeric value encountered in
C:\xampp\htdocs\telehealth\twilio_Sms\autometic.php on line 2
24
Now please anyone help what is my mistake and what should i change to correct this. I am trying many thins but i could not solve it
echo (date('d-m-Y')+"1");
The trouble is that the date('d-m-Y') returns string with - symbols and you try to sum it with a "1". You can't mix php types in this way.
First of all you definitely should read about php types, php math operations and type juggling.
Next, there is a special library to work with date and time. And you definitely should use it to date and time operations. The DateTime is an object that contents date and time and can be represented as string by format function.
Operations with date and time are quite difficult (leap years, February and so son). And it is highly recommended to use special API from DateTime.
So, for you task (refer to add function of datetime):
$date = new \DateTime('23-07-2019');
$date->add(new \DateInterval('P1D'));
echo $date->format('Y-m-d);
Can't figure out why the following returns false:
$timestamp = '1535552942';
if (time() <= strtotime((int)$timestamp + 10))) // time() = 1535553866
The current time IS greater than the timestamp + 10 s!!! I've checked in logs!
What's wrong with this?
Quoting the docs:
strtotime
Parse about any English textual datetime description into a Unix timestamp
In other words, you pass something that makes sense in English into this function - and expect timestamp as a result.
Instead you pass timestamp into the function, and strtotime just fails to get what you want from it. For example, this online demo shows int(31009790155) as result of strtotime((int)$timestamp + 10)) - which is in the future far far away.
Perhaps you meant it to be implemented this way:
$timestamp = '1535552942';
if ( time() <= strtotime('+10 seconds', $timestamp) ) { ... }
... but actually I fail to see why you can't just add 10 (as a number) to timestamp and then compare it against time() directly. These are the numbers, in the end.
According to the official website of PHP, the definition of function strtotime is....
strtotime
Parse about any English textual datetime description into
a Unix timestamp
What this definition says that you can provide Only English textual datetime. And you are providing it an Integer whereas this strtotime is looking for English textual.
When you will print return value of strtotime it will show your current timestamp.
$timestamp = 'now';
echo strtotime($timestamp);
But when you will try to print its return value with an integer argument, then it will nothing, as given below...
$timestamp = 1535552942;
echo strtotime($timestamp);
Because this function return timestamp on success otherwise return false.
When you will provide it English textual value and try to add one in argument, it will generate a warning...
$timestamp = 'now';
echo strtotime($timestamp+1);
Warning: A non-numeric value encountered in
Because, according to the PHP generates errors or warning on arithmetics with invalid strings.
New E_WARNING and E_NOTICE errors have been introduced when invalid
strings are coerced using operators expecting numbers or their assignment equivalents. An E_NOTICE is emitted when the string begins with a numeric value but contains trailing
non-numeric characters, and an E_WARNING is emitted when the string
does not contain a numeric value.
Whereas above-given snippet fulfill this condition.
So, now just change your value of $timestamp to "now" or any other valid English textual and remove int from your if condition, then it will work perfectly.
$timestamp = "now";
if (time() <= strtotime($timestamp)){
echo "it works";
}
I am going round in circles with this one! I'm doing the following:
Retrieving a date from an MSSQL datetime field via SQL/PHP
Sending the date to a new PHP page via the querystring
Trying to use that date in a new SQL query
I'm hitting problems here.
If I use echo:
echo $_REQUEST['rSessionDate'];
output: 15/10/2012
Which is fine, but when I use it in a SQL query I'm not getting the results I expect, so I thought the best thing to do would be to make sure it's being recognised as a date first.
If I use date_format():
echo date_format($_REQUEST['rSessionDate'],'Y-m-d');
output: Warning: date_format() expects parameter 1 to be DateTime, string given in ...
If I use strtotime():
echo strtotime($_REQUEST['rSessionDate']);
output: (nothing)
If I use date():
echo date('Y-m-d H:i',$_REQUEST['rSessionDate']);
output: Notice: A non well formed numeric value encountered in ...
If I use date() with strtotime():
echo date('Y-m-d H:i',strtotime($_REQUEST['rSessionDate']));
output: 1970-01-01 01:00
I'm sure I'm totally missing something simple.
EDIT: I've tried a few new functions I found:
$rSessionDate = new DateTime($_REQUEST['rSessionDate']);
echo $rSessionDate->format('Y-m-d H:i:s');
output: Fatal error: Uncaught exception 'Exception' with message 'DateTime::__construct(): Failed to parse time string (15/10/2012) at position 0 (1): Unexpected character'
and:
$rSessionDate = date_create($_REQUEST['rSessionDate']);
echo date_format($rSessionDate, 'Y-m-d H:i:s');
output: Warning: date_format() expects parameter 1 to be DateTime, boolean given i
EDIT 2:
I have tried using CAST:
SELECT fCourseCode ,fCourseTitle FROM tCourses WHERE fCourseCode = '4B' AND (ISNULL(fValidStart, 0) <= CAST('2012-10-15 00:00:00' as DATETIME))
But this fails with error "The conversion of a varchar data type to a datetime data type resulted in an out-of-range value"
These might help to shed some light on what you're looking for.
http://www.ozzu.com/programming-forum/php-mssql-datetime-field-not-pulling-correctly-t106226.html
http://af-design.com/blog/2010/03/13/microsoft-sql-server-driver-for-php-returns-datetime-object/
strtotime() is returning an epoch timestamp in your example above.
or check CAST and CONVERT (refers to MSSQL2000 but may still help you)
http://msdn.microsoft.com/en-us/library/aa226054%28SQL.80%29.aspx
if the date was retrieved from an MSSQL table and you want to use strtotime() in PHP and also don't want to change the date format to yyyy-mm-dd then you can use
CONVERT(VARCHAR(30), DateFromMSSQL, 121) as DateFromMSSQL
I'm trying to store a date from a PHP form into an MS SQL database and am really struggling. The date is in string format, and in 'DD/MM/YYYY' format so I've tried to use strtotime() to convert it, but I just get 1970-01-01 01:00:00 from that.
The code I've tried is the following:
$requested = $_REQUEST["TextboxRequiredBy"];
var_dump($requested);
echo "<br/>";
$daterequested = date("Y-m-d H:i:s", strtotime($requested));
echo $requested . " becomes " . $daterequested . "<br/>";
mssql_bind($stmt, "#Parameter", $daterequested, SQLFLT8, false);
What I get on screen is:
string(10) "31/07/2012"
31/07/2012 becomes 1970-01-01 01:00:00
Warning: mssql_bind(): Unable to set parameter in xxx.php on line 110
Warning: mssql_execute(): message: Procedure or function 'SPNAME' expects
parameter '#Parameter', which was not supplied. (severity 16) in xxx.php
I've searched around and I just can't seem to find a way to convert the string successfully. I've used the following on another page that runs a date comparison, but this brings back an error that date_format() expects parameter 1 to be DateTime, boolean given:
$datefrom = date_create($requested);
$daterequestedfrom = date_format($datefrom,'Y-m-d H:i:s');
Can anyone suggest how I can successfully convert the string into a date?
That date format is not a default format. You probably want to look at the date_create_from_format function. This will allow you to specify the format of the time string you are trying to input.
Link to PHP documentation
For some reason, PHP doesn't like the slashes in strtotime. Convert them to dots and the script should work.
$requested = str_replace('/', '.', $_REQUEST["TextboxRequiredBy"]);