Invalid numeric value

I have a form that passes two dates (start and end) in a PHP script that will add them to the database. I'm having trouble confirming this. I keep getting the following errors.

Invalid numeric value

This is when I use the following

date("d",$_GET['start_date']); 

But when I use the strtotime () function, as recommended by many sites, I get a unix time stamp of 1/1/1970. Any ideas how I can get the correct date?

+96
date php validation time
May 26 '11 at 9:30 a.m.
source share
9 answers

Because you pass the string as the second argument to the date function, which must be an integer.

string date (string $ format [, int $ timestamp = time ()])

Try strtotime, which will parse any text description of the date and time in English into a Unix timestamp (integer):

 date("d", strtotime($_GET['start_date'])); 
+189
Oct 03 '12 at 13:25
source share

$_GET['start_date'] not numeric, this is my bet, but the date format is not supported by strtotime . You will need to reformat the date to the working format for strtotime or use the explode / mktime combination.

I could add you an example if you are kind enough to post the format you are currently receiving.

+7
May 26 '11 at 9:34 a.m.
source share

You can simply solve this problem with the strtotime() function.

 date("d",strtotime($_GET['start_date'])); 
+6
Aug 05 '16 at 10:56 on
source share

I came across the same situation (in my case, the date in the PHP user field in the Drupal view), and what worked for me used intval instead of strtotime to turn the value into an integer because it was basically a timestamp. but as a string, not a whole. Obviously this does not apply to everyone, but it may be worth a try.

+5
Apr 15 '14 at 15:54
source share

It really helped me -

 $new_date = date_format(date_create($old_date), 'Ym-d'); 
+2
Nov 15 '16 at 10:28
source share

This is an old question, but there is another subtle way this message can happen. He explained pretty well here in the docs .

Imagine this scenario:

 try { // code that triggers a pdo exception } catch (Exception $e) { throw new MyCustomExceptionHandler($e); } 

And MyCustomExceptionHandler is defined something like this:

 class MyCustomExceptionHandler extends Exception { public function __construct($e) { parent::__construct($e->getMessage(), $e->getCode()); } } 

This will lead to a new exception in a special exception handler, because the Exception class expects a number for the second parameter in its constructor, but a PDOException can dynamically change the return type $e->getCode() to a string.

The workaround for this will be determined by your custom exception handler, for example:

 class MyCustomExceptionHandler extends Exception { public function __construct($e) { parent::__construct($e->getMessage()); $this->code = $e->getCode(); } } 
+1
Apr 23 '15 at 13:29
source share

If an error occurs during any calculation, double check that the values ​​do not contain a comma (,). Values ​​should only be in integer / float format.

0
Mar 19 '18 at 7:23
source share

You need to set the time zone using date_default_timezone_set ().

0
Mar 23 '18 at 18:18
source share

Passing a string is not necessarily a problem if it contains only numbers.

Another reason this can happen is if you have a place before or after. For example, "1557399276"

0
May 9 '19 at 10:55
source share



All Articles