I have this PHP code:
echo "time30: ".$time30.'<br />';
echo "time: ".$time.'<br />';
echo date("s",strtotime($time) - strtotime($time30));
Which is returning:
time30: 15/09/2015 20:27:16
time: 15/09/2015 21:08:41
00
Why is it not returning the difference instead of 00?!
Subtracting 2 timestamps doesn't get you a timestamp. It gets you the difference between them. You are then trying to read that difference as a timestamp (or an exact moment in time), and it just to happens to have a "seconds" value of 0.
If you want the number of seconds between these two times, then you just need to do:
echo strtotime($time) - strtotime($time30);
P.S. 15/09/2015 20:27:16 is not a format that strtotime recognizes. I suggest you use DateTime objects for this task.
$time = DateTime::createFromFormat('d/m/Y H:i:s', '15/09/2015 21:08:41');
$time30 = DateTime::createFromFormat('d/m/Y H:i:s', '15/09/2015 20:27:16');
$diff = $time->diff($time30);
$minutes = ($diff->days*24*60) + ($diff->h*60) + ($diff->i);
$seconds = ($minutes*60) + $diff->s;
echo $seconds; // 2485 (which is 41 minutes, 25 seconds)
instead of using
$time30= '15/09/2015 20:27:16';
$time= '15/09/2015 21:08:41';
use
$time30= '2015-09-15 20:27:16';
$time= '2015-09-15 21:08:41';
Format has to be correct http://php.net/manual/en/function.strtotime.php
Related
I have this 13 digit timestamp 1443852054000 that i want to convert to date and time but dont succeed. I have tried this codes:
echo date('Y-m-d h:i:s',$item->timestamp);
doesnt work for me and also this
$unix_time = date('Ymdhis', strtotime($datetime ));
and this :
$item = strtotime($txn_row['appoint_date']);
<?php echo date("Y-m-d H:i:s", $time); ?>
what should i use?
This timestamp is in milliseconds, not in seconds. Divide it by 1000 and use date function:
echo date('Y-m-d h:i:s', $item->timestamp / 1000);
// e.g
echo date('Y-m-d h:i:s',1443852054000/1000);
// shows 2015-10-03 02:00:54
A 13 digit timestamp is used in JavaScript to represent time in milliseconds. In PHP 10 a digit timestamp is used to represent time in seconds. So divide by 1000 and round off to get 10 digits.
$timestamp = 1443852054000;
echo date('Y-m-d h:i:s', floor($timestamp / 1000));
You can achieve this with DateTime::createFromFormat.
Because you've a timestamp with 13 digits, you'll have to divide it by 1000, in order to use it with DateTime, i.e.:
$ts = 1443852054000 / 1000; // we're basically removing the last 3 zeros
$date = DateTime::createFromFormat("U", $ts)->format("Y-m-d h:i:s");
echo $date;
//2015-10-03 06:00:54
DEMO
http://sandbox.onlinephpfunctions.com/code/d0d01718e0fc02574b401e798aaa201137658acb
You may want to set the default timezone to avoid any warnings
date_default_timezone_set('Europe/Lisbon');
NOTE:
More about php date and time at php the right way
I am using this code to calculate the time period. In fact, I want the period between two HH:MM:SS moment and have a result in HH:MM:SS format.
$time1 = strtotime('00:00:00');
$time2 = strtotime('00:00:07');
$diff = $time2 - $time1;
$diff = date('H:i:s', $diff);
I'm expecting 00:00:07 but I get 01:00:07. What could be the problem?
Irvin run here, and get right answer but I run the same code on my local machine and wrong!!
Is it timezone or maybe some configuration effect the result?!
Please stop using strtotime and date functions. Use the DateTime class.
And if you would have searched better, finding difference for two dates has already been all over SO. For instance, my answer here. Slightly changed it would look like:
$create_time = "00:00:00";
$current_time="00:00:07";
$dtCurrent = DateTime::createFromFormat('H:i:s', $current_time);
$dtCreate = DateTime::createFromFormat('H:i:s', $create_time);
$diff = $dtCurrent->diff($dtCreate);
echo $diff->format("%H:%I:%S"); // to get HH:MM:SS format
This returns 00:00:07
See DateInterval::format for more formatting details.
It's because when you minus time 1 from time 2 you end up with 7;
The date function needs a timestamp, 7 is not a timestamp.
You could use http://php.net/manual/en/datetime.diff.php to calculate the difference instead?
You should consider the UNIX epoch which cancels timezone diffrences in ini settings, essentially. it's Jan 1st '70. You can check your server/app's setup for timezone by adding e, O or P. Check this code:
$time1 = strtotime('00:00:00 +0000');
$time2 = strtotime('00:00:07 +0000');
$epoch = strtotime('01 Jan 1970');
$diff = $time2 - $time1;
var_dump($time1,$time2,$epoch,$diff);
$diff = date('H:i:s', $epoch+$diff);
echo $diff;
How would you convert time to integer?
string(8) "04:04:07"
I want this as 4(hours) or much better 4.04(4hours and 4 minutes)
I tried
$yourdatetime = "04:04:07";
$timestamp = strtotime($yourdatetime);
Which results in
int(1458590887)
The date function is your friend:
Given your own code above:
$yourdatetime = "04:04:07";
$timestamp = strtotime($yourdatetime);
You can then feed it into the date function:
echo 'Hours:' . date('h', $timestamp); // Hours: 04
echo 'Minutes:' . date('i', $timestamp); // Minutes: 04
echo 'Seconds:' . date('s', $timestamp); // Seconds: 07
Refer to the docs for the specific format(s) you'd like for hours - there's many options.
You could even do it in one move:
echo date('h.i', $timestamp); // 04.04
If you need it truly numeric:
echo float(date('h.i', $timestamp)); // 4.04
Use (float) and preg_replace to one-line-code conversion:
$floatval = (float) preg_replace('/^(\d+):(\d+).+/','\1.\2',$yourdatetime);
So we'll break this out. If we cast the hours as an integer and leave the minutes as a string this is a pretty simple conversion
$time = explode(':', $yourdatetime);
$hours = (int)$time[0] . '.' . $time[1];
Avoids any overhead from regex
strtotime() returns the time in seconds since the Unix Epoch. You can then format this using date(). Documentation for date: http://php.net/manual/en/function.date.php
To get the number of hours (4):
$timestamp = date("g",strtotime($yourdatetime));
To get the number of hours and minutes (4.03):
$timestamp = date("g.i",strtotime($yourdatetime));
I have a value 15 min from which i need to get H:i:s. Tried date('H:i:s', strtotime('15 min')) but it returns 15:23:47 and i need 00:15:00.
How about:
<?php
$time = "15 min";
echo date('H:i:s', strtotime("midnight + " . $time));
?>
You have many solutions for getting the H:i:s from minutes.
<?php
echo date('H:i:s', strtotime("midnight + 15 min"));
?>
<?php
echo date('H:i:s', strtotime("today + 15 min"));
?>
<?php
echo date('H:i:s', strtotime("tomorrow + 15 min"));
?>
Your value of '15 mins', in this case is not a time, it is a time interval and is most appropriately represented by the DateInterval class. Using the right tool makes the job very simple:-
$interval = \DateInterval::createFromDateString('15 min');
echo $interval->format('%H:%I:%S');
Output:-
00:15:00
You could get the same output in one line if you are a fan of that kind of thing:-
echo \DateInterval::createFromDateString('15 min')->format('%H:%I:%S');
However the first option has the advantage of keeping the DateInterval instance available for further use.
this is a very lame question but i m not able to find this one.
How to get today's date and date after two months..
format is month-date-year (numerical.)
You can use the strtotime() function :
$today = time();
$twoMonthsLater = strtotime("+2 months", $today);
// If what you really want is exactly 60 days later, then
$sixtyDaysLater = strtotime("+60 days", $today);
// ...or 8 weeks later :
$eightWeeksLater = strtotime("+8 weeks", $today);
In any case, the resulting new timestamps can then be converted to month-date-year :
echo 'Today is : ' . date('m-d-Y', $today);
echo 'Two months later will be : ' . date('m-d-Y', $twoMonthsLater);
** UPDATE **
From the PHP manual
Note: Please keep in mind that these functions are dependent on the locale settings of your server. Make sure to take daylight saving time (use e.g. $date = strtotime('+7 days', $date) and not $date += 7*24*60*60) and leap years into consideration when working with these functions.
Just thought I should mention it...
There are a couple of ways you could go about it - the first one would be to do something like this:
echo $currentdate = date("Y-m-d H:i:s",time());
echo $after60days = date('Y-m-d H:i:s', time() + 60 * 60 * 24 * 60);
Basically, you take the current timestamp, expressed in seconds, and add 60 * 60 * 24 * 60, which is the amount of seconds in two months.
Another way to do it, which is my preferred way and how I would do it, is this:
$after60days = strtotime("+60 days");
The outcome will be exactly the same, $after60days will have a value equal to the timestamp of the day exactly two month from now, but it uses PHP's own strtotime() function.
Of course, if you need to output a date in a format that easy to read for humans, you can do something like this:
echo date('Y-m-d H:i:s',$after60days);
Today:
date('m-d-Y', time());
Two months from now:
date('m-d-Y', time() + (86400 * 60));