Time format not consistently correct via API

Hi
I am building a web solution for a company to send smses from a form on the company intranet.

The process is:
user enters number and message on form

The following executes after logging in to MT via the API

$API->write("/tool/sms/sent/print");

	 $READ = $API->read(true);
    if (!empty($READ)) {                                        
        foreach ($READ as $values) {
		$phone = $values['phone'];
		$message = $values['message'];
		}
	
	$API->write("/log/print
	?topics=gsm, info");

	$READ = $API->read(true);
    if (!empty($READ)) {                                        
        foreach ($READ as $values) {
		$status = $values['message'];
		$miktime = $values['time'];
    }
	
	$API->write("/system/clock/print");
	$READAPI = $API->read(true);
    if (!empty($READAPI)) {                                        
        foreach ($READAPI as $value) {
		$miknowtime = $value['time'];
    }

I then use the generated variables to update a mysqli database.

However, when I submit the form the first time,
the returned value for $miktime is sep/17 13:05:17
and the value for $miknowtime is 03:09:33

and when I submit the form a second time with the same values but at a different time
the returned value for $miktime is 03:09:34
and the value for $miknowtime is 05:17:13

There are 2 anomalies here which I please need help resolving:

  1. The first iteration includes the date as well as the time for $miktime but the second iteration correctly has only the time.
  2. The value for $miktime is not the last entry in the mikrotik logs but a previous entry in the logs.

Please can someone explain how I can consistently get only the time without the date for the last sent sms log entry and also how to get the correct sms sent from the log.
Should I be filtering differently here?

$API->write("/log/print
	?topics=gsm, info");

I am trying to avoid using a log server but am open to any suggestions. Thanks!

It appears that this is a timing issue with the read/write on the Mik.

I added a sleep timer to the PHP code and that seems to have resolved the issue. I am now consistently getting the expected variable values.

Code changed to this and all seems well:

$READ = $API->read(true);
    if (!empty($READ)) {                                        
        foreach ($READ as $values) {
		$phone = $values['phone'];
		$message = $values['message'];
		}

// This line of code added //		
sleep(2);

$API->write("/log/print
	?topics=gsm, info");

	$READ = $API->read(true);
    if (!empty($READ)) {                                        
        foreach ($READ as $values) {
		$status = $values['message'];
		$miktime = $values['time'];
    }