Home>

This article shares with you the time stamp and time conversion (php.net accurate to milliseconds), interested friends to learn together

/** get the current timestamp,Accurate to milliseconds * /
function microtime_float ()
{
  list ($usec, $sec)=explode ("", microtime ());
  return ((float) $usec+ (float) $sec);
}
/** format the timestamp,Accurate to the millisecond,x stands for milliseconds * /
function microtime_format ($tag, $time)
{
  list ($usec, $sec)=explode (".", $time);
  $date=date ($tag, $usec);
  return str_replace ("x", $sec, $date);
}
</pre>
</div>
<p>
Instructions:
</p>
<p>
1. Get the current timestamp (accurate to the millisecond):microtime_float ()
</p>
<p>
2. Timestamp conversion time:microtime_format ("y year m month d day h hour i minutes s seconds x milliseconds", 1270626578
</p>
<p>
.net timestamp conversion to the nearest millisecond
</p>
<p>
Record a mutual conversion method of timestamps here,I looked for it online.
Basically not accurate to the millisecond,My basic can meet the level of milliseconds,code show as below:
</p>
<div>
<pre>
 ///<summary>
     ///unix timestamp converted to datetime
     ///</summary>
     private datetime converttodatetime (string timestamp)
     {
       system.datetime time=system.datetime.minvalue;
       //Accurate to milliseconds
       //Time stamp is converted into time
       datetime start=timezone.currenttimezone.tolocaltime (new system.datetime (,,));
       try
       {
         time=timestamp.length ==?start.addseconds (long.parse (timestamp)):start.addmilliseconds (long.parse (timestamp));
       }
       catch (exception ex)
       {
         return start;//Conversion failed
       }
       return time;
     }
     ///<summary>
     ///datetime converted to unix timestamp
     ///</summary>
     ///<param name="time"></param>
     ///<returns></returns>
     private string converttimestamp (datetime time)
     {
       double intresult =;
       system.datetime starttime=timezone.currenttimezone.tolocaltime (new system.datetime (,,));
       intresult=(time-starttime) .totalmilliseconds;
       return math.round (intresult,). tostring ();
     }
c
  • Previous Batch processing to import the file names in File Mania into the corresponding names in the table
  • Next Jsp + jdbc method for connecting to a database