A non well formed numeric value encountered from XML feed? - php

I am getting feeds from XML feeder and when i try to convert it's date values, i am getting notice:
A non well formed numeric value encountered on line 61
Feeds returns date in this format:
24.06.2015
and time in this format:
12:00
I tried to convert it like this:
$dbdate = date('Y-m-d',strtotime($match->attributes()->date));
$dbtime = date('h:i', strtotime($match->attributes()->time));
$datetime = date('Y-m-d h:i',$dbdate . ' ' . $dbtime);
In database it is converted as i want, but i get notice before that.
What i am doing wrong?
UPDATE
SOLVED, i forget to add strtotime in $datetime :
$datetime = date('Y-m-d H:i',strtotime($dbdate . $dbtime));

Related

PHP date_create_from_format using with object giving bool(false) error

I am trying to use the PHP date_create_from_format on an object so I can use the diff() function to calculate the number of days from when the record was created to today (whenever that is looked at)
If I echo $job->created_date I get something like 2021-2-27 10:05:00
I started with:
$created = $job->created_date;
$date_now = date('Y-m-d');
$job_posted_date_difference = $date_now->diff($created); <<< the error shows on this line
echo $job_posted_date_difference.$difference->d.' days';
but I get a fatal error Call to a member function diff() on string
So....... I thought that it could be a formatting of the initial date issue and then I tried
$created = date_create_from_format('Y-m-d', $job->created_date);
$date_now = date('Y-m-d');
var_dump ($created);
But the dump shows - bool(false)
How do I fix this properly? I looked at a number of other SO issues and they seemed to really range from the dates being in the wrong order( I tried both ways) to the formatting issue .
and if you want to calculate days from two dates
then you can use
$ceated_date = "2021-2-27 10:05:00";
//create a date object of created date and get timestamp.
$t1 = (new DateTime($created_date))->getTimetamp();
//create a date object of now and get timestamp.
$t2 = (new DateTime("now"))->getTimestamp();
//this will give you days
$day_diff = floor(($t2 - $t1) / 86400);

Converting dates from xpath with strtotime but keep getting epoch

Trying to convert dates being scraped with xpath to mysql datetime format and having some trouble. The below is my date, and my function to convert the date, and my desired output:
$date = ' 8-May-2014';
$date_fixed = date("Y-m-d H:i:s", strtotime(trim($date)));
echo $date; // ' 8-May-2014'
echo $date_fixed; // '2014-05-08 00:00:00'
The issue is that when I run this in my server (php7), and the date is set from an xpath nodeValue, the conversion comes back at the epoch time instead, even though the value of that node is the date string. I var_dump'ed the nodeValues to verify they are strings, and they are. I tried setting the nodeValue to a new variable and then converting that, still doesn't work.
$date = $xpath->query('./td[2]', $row)->item(0)->nodeValue; // ' 8-May-2014'
$date_fixed = date("Y-m-d H:i:s", strtotime($date));
echo $date_fixed; // '1970-01-01 00:00:00'
I'm not sure what I'm missing.
Turned out to be non-unicode characters. Not sure why the browser or php or xpath returned them as spaces, but eliminated them with:
substr($date, 4);

Generate a data-time format from 2 separated strings

I need to compare date/time that I get from MySQL with a format like: 2013-05-17 15:07:29
From another database, I have data and time separated and in the notation: 130998 081836
I have concatenated the two strings to get only one and I'm trying to convert it to my desired format using:
$dateTimeNmea = $array[9]." ".$array[1]; // 130998 081836
$dateTime = date("Y-m-d H:i:s", $dateTimeNmea); // 1970-01-02 13:23:18
So "it works" on the format but the values are wrong. It could be 1998-09-13 08:18:36
Where is my fault?
It has format siH dmy. Try date_parse_from_format('siH dmy', $string) to get it in array.
130998 081836 is not a format for a date that the date function can understand.
For starters the date() function expects the second param to be a timestamp (read docs for it here)
Then you would need to parse the string into a useable date format via date_parse_from_format and finally into a timestamp
Something like
$string = '130998 081836';
$date = date_parse_from_format('dmY His', $string);
$dateString = date('Y-m-d H:i:s', strtotime(vsprintf('%s-%s-%s %s:%s:%s', $date)));
var_dump($dateString); // var dump just for output/test
The strtotime(vsprintf('%s-%s-%s %s:%s:%s', $date) formats your parsed date into a timestamp which can then be used in date methods second param to get exactly the format you need.

Date conversion and updating mongodb document using PHP

Progress :
1. I retireved date from a collection.
Example format : Fri Oct 05 14:59:31 +0000 2012
2. I was able to change its format.
CODE USED :
$cur=$col->find(array(),array("created_at"=>1,"_id"=>0));
// created_at = contains Date value
$cur_all=$col->find();
while($doc=$cur_all->getNext())
{
$doc2=$cur->getNext();
$pieces = implode(" ", $doc2);
//converted the array to string with space delimiting
if($pieces!=NULL)
{
$date1 = date_create_from_format("D M d G:i:s +O Y", $pieces);
echo date_format ( $date1 , 'Y-m-d G:i:s' );
//This is the format i would like to update in mongodb..
$filter = array('_id'=>new MongoId($doc['_id']));
$update = array('$set'=>array('created_at'=> newMongoDate($date2)));
$col->update($filter,$update);
}
}
QUESTION :
Where to create a date object so that it could be updated to the documents in the collection in the expected format? (format : Y-m-d G:i:s )
P.S : I did a lot of research on Stackoverflow (And other places, as well.) but I could not come to any conclusions. That is why this question. Let me know if there are any clarifications
Hmm even though you have explained your background well your actual question:
Where to create a date object so that it could be updated to the documents in the collection in the expected format? (format : Y-m-d G:i:s )
Is a bit confusing.
MongoDB will always save the date in one format and one format only when using ISODate: http://en.wikipedia.org/wiki/ISO_8601 (otherwise known as MongoDate in PHP) and it is probably best to not mess with this status quo.
So I would recommend you use the format Y-m-d G:i:s only as display, i.e.:
$date1 = new MongoDate();
var_dump(date('Y-m-d G:i:s', $date1->sec));
And you use the original $date1 object to actually save to the database.
Of course this would change if you were to create a date in your specified format however here is a piece of code for an example:
$date1 = new MongoDate();
$date2 = new MongoDate(strtotime(date ( 'Y-m-d G:i:s', $date1->sec )));
var_dump(date('Y-m-d G:i:s', $date2->sec));
You can use the $date2 as the date to save into MongoDB formed from the specific format you want.
look at http://php.net/manual/en/class.mongodate.php
your code should create a date using a unix timestamp
$date2 = ('23rd April 2013');
$update = array('$set'=>array(
'created_at'=> new MongoDate(strtotime($date2))
));
http://www.php.net/manual/en/function.strtotime.php

mysql timestamp conversion/formatting Notice: A non well formed numeric value encountered

In my database I have set row "posted" as a timestamp but I get this notice when trying to convert/format it:
Notice: A non well formed numeric value encountered
code:
$posted = date('d/m/Y H:i:s', $row['posted']);
echo $posted;
what am I doing wrong?
This means that the second parameter for date() is expecting integer, so convert $row['posted'] to timestamp first.
Try
$posted = date('d/m/Y H:i:s', strtotime($row['posted']));

Categories