Getting Date Format M-D-Y H:I:S.U from Milliseconds

Getting date format m-d-Y H:i:s.u from milliseconds

php.net says:

Microseconds (added in PHP 5.2.2). Note that date() will always generate 000000 since it takes an integer parameter, whereas DateTime::format() does support microseconds if DateTime was created with microseconds.

So use as simple:

$micro_date = microtime();
$date_array = explode(" ",$micro_date);
$date = date("Y-m-d H:i:s",$date_array[1]);
echo "Date: $date:" . $date_array[0]."<br>";

Recommended and use dateTime() class from referenced:

$t = microtime(true);
$micro = sprintf("%06d",($t - floor($t)) * 1000000);
$d = new DateTime( date('Y-m-d H:i:s.'.$micro, $t) );

print $d->format("Y-m-d H:i:s.u"); // note at point on "u"

Note u is microseconds (1 seconds = 1000000 µs).

Another example from php.net:

$d2=new DateTime("2012-07-08 11:14:15.889342");

Reference of dateTime() on php.net

I've answered on question as short and simplify to author. Please see for more information to author: getting date format m-d-Y H:i:s.u from milliseconds

How to convert iso date time with milliseconds to date time format in php so that milliseconds don't go away?

Use DateTime because strtotime and date only uses full seconds.

$date = new DateTime('2020-03-03T11:07:41.1708478Z');
echo $date->format("Y-m-d H:i:s.u"); // 2020-03-03 11:07:41.170847

https://3v4l.org/DXVj8

But since this I assume this input format is rater fixed and wont change I could recommend you to use a simple str_replace.

echo str_replace(["T","Z"], [" ",""], '2020-03-03T11:07:41.1708478Z'); // 2020-03-03 11:07:41.170847

Format date in PHP MMddyyyyHHmmss to m-d-y

echo date('m-d-Y', 1568145593000 / 1000);

Divide the timestamp by 1000 to get the timestamp in seconds.

See:

PHP date https://www.php.net/manual/en/function.date.php
and for the formatting characters: https://www.php.net/manual/en/datetime.format.php (scroll down a bit)

DateTime with microseconds

/!\ EDIT /!\

I now use https://github.com/briannesbitt/Carbon, the rest of this answer is just here for historical reasons.

END EDIT

I decided to extend the class DateTime using the tips you all gave me.

The constructor takes a float (from microtime) or nothing (in this case it will be initialized with the current "micro-timestamp").
I also overrided 2 functions that were important : setTimestamp and getTimestamp.

Unfortunately, I couldn't solve the performances issue, although it's not as slow as I thought.

Here's the whole class :

<?php
class MicroDateTime extends DateTime
{
public $microseconds = 0;

public function __construct($time = 'now')
{
if ($time == 'now')
$time = microtime(true);

if (is_float($time + 0)) // "+ 0" implicitly converts $time to a numeric value
{
list($ts, $ms) = explode('.', $time);
parent::__construct(date('Y-m-d H:i:s.', $ts).$ms);
$this->microseconds = $time - (int)$time;
}
else
throw new Exception('Incorrect value for time "'.print_r($time, true).'"');
}

public function setTimestamp($timestamp)
{
parent::setTimestamp($timestamp);
$this->microseconds = $timestamp - (int)$timestamp;
}

public function getTimestamp()
{
return parent::getTimestamp() + $this->microseconds;
}
}

How to sum start time in date format and run time in milliseconds to find end time in unix script?

paste start_time.txt execution_time.txt | while IFS="$(printf '\t')" read -r f1 f2
do

# convert execution_time in seconds from ms
execution_time_ms="$(echo $f2 | cut -d'm' -f1)"
execution_time_ms="$(echo $execution_time_ms | cut -d's' -f1)"
execution_time_s=$(awk "BEGIN {printf \"%.10f\",$execution_time_ms/1000}")

# create dateformat string to add seconds
date_format=$f1
date_format+=" +$execution_time_s"
date_format+="seconds"

# create end date using dateformat string
end_time=`date --date="$date_format" +'%Y-%m-%d %H:%M:%S'`
end_time="${end_time/ /T}"
end_time+='Z'

# append end time
echo $end_time >> end_time.txt

done

This is a script which solves your problem.
I have used 3 files as per you mentioned.

  • start_time.txt (Input file)
  • execution_time.txt (Input file)
  • end_time.txt (Output file)

how to produce the postgresql date output in php?

That part is microsecond, you can use this format Y-m-d H:i:s.uO to add microsecond.

One thing to note that microsecond format with date() will always be 000000. Use DateTime::format() instead:

$date = new DateTime();
echo $date->format('Y-m-d H:i:s.uO');

Output:

2020-12-07 03:17:37.011493+0100

Print datetime with microseconds from DB

Your database format is supported in DateTime::__construct(), so you can simply pass it as a parameter:

$date = new DateTime($order["DateTimeEnteredMicro"]);
print $date->format("Y-m-d H:i:s.u");

Note that microseconds work in PHP >= 5.2.2. Timezone issues may arise if MySQL and PHP don't have the same timezones set.

P.S.
This is where you were going wrong in your code:

  1. $t has the current time, so later when you use it as the second parameter to date(), it was being used instead of your db value.
  2. $micro = date("Y-m-d H:i:s",strtotime($order["DateTimeEnteredMicro"])); - this made the $micro variable a full date, instead of just the microsecond part.
  3. This didn't make much sense either: date('Y-m-d H:i:s.'.$micro, $t) since here you were trying to append a value to the format.


Related Topics



Leave a reply



Submit