i'm wondering if there's a way to get any datetime in microseconds.
I was looking forward microtime(), but it just returns the date in the moment.
Anyone knows if is this possible?
I have my date given like: Y-m-d H:i:s.u.
I was thinking about something like (Y-1970)*31556926 + m*151200+d*86400+h*3600+m*60+s.u
but I don't know if that's why i'm a beginner on programming, but i can't think in a way to separate each: Y,m... to do the math.
Would appreciate any help/suggestions.
You can do this with DateTime:
$date = DateTime::createFromFormat('Y-m-d H:i:s.u', '2000-01-01 13:12:12.129817');
echo $date->format('U.u');
//prints: 946728732.129817
What I would do is simply split apart your input format like this:
Y-m-d H:i:s and u
You should be able to do this by exploding on . in your input formatted date string. Then just calculate the UNIX timestamp on the whole second portion. Finally just add your fraction second portion back to the timestamp (via either string concatenation of arithmetic depending on whether you want string or float as result).
No it's not possible. Timestamps are integers internally, describing the seconds from 01.01.1970 GMT. Regardless if you are using a 32 or 64 bit system, the microseconds from 1970 would lead to an overflow as there has much too many of them gone.
You updated the question.. Yes, it is possible to display the current time in microsends using microtime():
$microtime = microtime();
// will return something like:
// 0.40823900 1381181037
// where the first are the micros and the last a regular time stamp
list($micros, $timestamp) = explode(' ', $microtime);
echo date('Y-m-d H:i:s', intval($timestamp)) . '+'
. (floatval($micros) * 1000000) . 'msec';
Related
Because using timestamp leads to a better performance in mysql I want to save the time as timestamp in my database. I receive a datetime string like this: 2014-09-24 17:18:27, convert it to timestamp with strtotime($the_date_from_above) and just for fun I converted it back with date("Y-m-d H:m:s", strtotime($the_date_from_above))
I var_dumped the process:
string(19) "2014-09-24 17:18:27"
int(1411579107)
string(19) "2014-09-24 17:09:27"
What went wrong during the time string convert? What is causing the difference? It's the same amount of seconds but 9 minutes difference is strange.
You'll need to convert it back like this:
date("Y-m-d H:i:s", strtotime($the_date_from_above))
When you convert it your way the m in H:m:s gets treated as the month.
Even nicer would be to use the datetime class:
$date = new DateTime();
$date = $date->setTimestamp($yourtimestamp);
echo $date->format("Y-m-d H:i:s");
date("Y-m-d H:m:s", strtotime($the_date_from_above))
^-----^---
you use m twice, which is the MONTHS value. Minutes are actually i.
You simply use wrong date-time template "Y-m-d H:m:s" should be "Y-m-d H:i:s". Little mistake.
But what I don't understand is your reason to do it this way. Because MySql has correct column type for saving Date-Time values and type timestamp is usually use for another purpose.
For better performance and precision, if you want to use value for next calculation, can sense save in database this value as string. (means timestamp with miliseconds)
I am new to PHP and I am trying to learn more of php date and time but I seem to get stuck with this.
I have this date format:
ddMMyyHHmmss
And an example is 120813125055 but I am trying to manipulate the string such that it will give me the format of:
yyyy-MM-dd HH:mm:ss (in the example above, 2013-08-12 12:50:55)
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
But it always gives me a result of 1969-12-31 18:00:00.
I assume that I need to do some string manipulation in PHP for this but I was wondering if there is an easier and more efficient way to do it?
I think what you're looking for is in the second response answered here: how to re-format datetime string in php?
To summarize (and apply to your example), you could modify the code like this.
$datetime = "120813125055";
$d = DateTime::createFromFormat("dmyHis", $datetime);
echo $d->format("Y-m-d H:i:s");
Use date_create_from_format:
$ts = date_create_from_format('dmyHis', '120813125055');
$str = date('Y-m-d H:i:s', $ts);
strtotime() only works on EASILY recognizable formats. Your is a ugly mix of garbage, so no surprise that strtotime bails with a boolean FALSE for failure, which then gets typecast to an int 0 when you tried feed it back into date().
And of course, note that your time string is NOT y2k compliant. two digit years should never ever be used anymore, except for display purposes.
You're using your function call and the argument the wrong way around.
In your example, php will try to return you the date for which the time is 'strtotime('120813125055')', and this function returns false (interpreted as 0). So you get returned the date formatted in 'Y-m-d H:i:s' for the Unix epoch.
You will need to get the actual timestamp of your string, so use http://www.php.net/manual/en/datetime.createfromformat.php.
You are mistaken here..
I tried to do something like:
date('Y-m-d H:i:s', strtotime('120813125055'));
You shouldn't use only numbers ( doesnt matter its an integer or a string ), than it will always give you the same thing.
You can use any other valid date and time ( E.G. 6 Jun 2013, 5 may 12...) . Because what strtotime() do is detect a valid date and convert it into timestamp.
I need some assistance.
The code uses a date() function to acquire the report date, while the availability times are manually entered by users. The availability times are then compared to a static time in order to show whether the process was complete on time.
This process works perfectly if all processes run on the same day; however inaccurate reporting occurs when the process completes before midnight of the previous day. This is where I need some assistance.
I need to take the time string, which is set as H:i format, and merge it with the date string, which is set as yyyy/mm/dd. Once this is performed, I can compare it to the report date stamp to get proper reporting.
Concat the two strings respectively, date and time then insert the result string into strtotime()
$date = '2012/06/15';
$time = ' 10:33';
echo strtotime($date.$time)
concatenation operator in PHP is a dot, "."
and you can always concatenate whatever date with whatever time
$date = "2012/01/04";
$time = "22:12";
$datetime = $date." ".$time;
then you can compare the latter variable with whatever datetime of the same format
Something like this might point you in the right direction:
<?php
$date = "2013/01/04";
$time = "16:58"; // I'm assuming you meant H:i format (not capital I as in your question).
$timestamp = strtotime($date." ".$time);
echo $timestamp; // 1357318680
?>
I'm trying to convert an epoch timestamp with php 5.3 with the following statement
date('d-m-Y',strtotime('1349042399999'));
to human readable format and getting wrong result: 01-01-1970what should return30-09-2012. I have been searching around and founding the following topic PHP strtotime returns a 1970 date when date column is null but did not help on my case.
The reason for that is that there are milliseconds embedded in that timestamp, which causes it to go over the integer overflow limit.
chop the last 3 characters, and you're good to go:
$original_timestamp = "1349042399999";
$timestamp = (int) substr($original_timestamp,0,-3);
echo date('d-m-Y',$timestamp);
By using strtotime, you are trying to convert a timestamp into another timestamp. Your timestamp is also in microseconds. The date function just needs the timestamp without the microseconds like so:
date('d-m-Y',substr('1349042399999', 0, -3));
I believe that the Second Rikudo solution worked because of the cast to int, not because of trimming the milliseconds.
It seems to me that this should work for you (it did for me)
$original_timestamp = "1349042399999";
date('d-m-Y', (int) $original_timestamp);
Okay so I have an array of results from a table, we have a start and end time we are retrieving. the start/end time would look something like this:
1345497551
Now I'm trying to convert this to a real time, for instance 1345497551 might become 2012/05/09 17:23:12 or something. I've found a few things but none seem to work correctly. one solution I tried, according to what someone was saying on another question on here, was
$createdate = date('H:i:s',$numberofsecs);
where $numberofsecs was the time pulled in from the array. but this only ever outputs 17:00:00 repeatedly for every time we had available for testing.
How can I go about making this work correctly?
Assuming that that's a standard unix timestamp string (seconds since midnight 1/1/1970), then you should be able to use date as you mentioned, but just modify the format string:
echo date('Y/m/d H:i:s', $numberofsecs);
The example you mention where you were always getting 17:00:00 could have been because your test cases were all only datestamps, encoded as timestamps, and having an offset from GMT . . .
I have tried below code:
$ts = 1345497551;
$date = new DateTime("#$ts");
echo $date->format('U = Y-m-d H:i:s');
output : 1345497551 = 2012-08-20 21:19:11