Re: How to insert : and - into timestamp string from IoT device?
| From: | Bo Berglund | Date: | Fri, 16 Dec 2022 15:32:58 +0000 |
| Subject: | Re: How to insert : and - into timestamp string from IoT device? | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-328106@lists.php.net to get a copy of this message | ||
On Fri, 16 Dec 2022 10:31:59 +0000, Jànis Elmeris <janis.elmeris@lv.lv> wrote:
>>No: Bo Berglund <bo.berglund@gmail.com>
>>Nosþtïts: piektdiena, 2022. gada 16. decembris 10:23
>>Kam: php-general@lists.php.net <php-general@lists.php.net>
>>Tºma: How to insert : and - into timestamp string from IoT device?
>>
>>I have an Arduino IoT device which reads data from an electricity meter and
>>posts to a website database through a web call to a php script on my webserver.
>>THis works fine and the timestamp gets into the database as expected.
>>However it is hard to read so I want to add separators before storing onto the
>>database.
>>
>>The timestamp retrieved from the meter looks like this example:
>>221216085802
>>
>>It is sent by adding parameter ×tamp=221216085802 to a get call to the php
>>script.
>>
>>In php on the webserver before I store it into the database I want to convert it
>>to a more readable format so it looks like this:
>>
>>22-12-16 08:58:02
>>
>>How can I do this in php code?
>>
>>I.e insert two - one space and two : into the string at given positions
>Here are two ways:
>
><?php
>
>$timeraw = '221216085802';
>$datetime = DateTime::createFromFormat('ymdHis', $timeraw);
>print $datetime->format('y-m-d H:i:s');
>print "\n";
>print $timeraw[0] . $timeraw[1] . '-' . $timeraw[2] . $timeraw[3] . '-' .
>$timeraw[4] . $timeraw[5] . ' ' . $timeraw[6] . $timeraw[7] . ':' . $timeraw[8]
>. $timeraw[9] . ':' . $timeraw[10] . $timeraw[11];
>
>Regards,
>Janis
Well, I need to convert what has been received by inserting - chars after the
2nd and 4th char in the received data and then a space after the 6th and a :
following the 8th and 10th char...
And I do not want to print anything I want to have it in a variable which can be
used in the database storage query.
Anyway, I figured out a way to reprogram the IoT firmware such that it does this
*before* sending it out to the webserver.
And to circumvent the problem of sending spaces in web calls I used an
underscore between the date and time part instead.
--
Bo Berglund
Developer in Sweden