How to get H:i:s from minutes? - php

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.

Related

How to display time in PHP by date() function

I get the value 9.0 from a CSV file that I would like to print as the time 09:00:00
If i try
<?php
$time = 9.01;
echo date('H:i:s', strtotime($time));
It displays 09:01:00.
But if I try
<?php
$time = 9;
echo date('H:i:s', strtotime($time)); or echo date('H:i:s', strtotime(9.00));
It displays 01:00:00.
I want to get time 09:00:00.
Please try this
$time = 9;
echo date( 'H:i:s', strtotime( number_format($time, 2)));
This is how to fill a date variable with any value you need:
<?php
$d=mktime(9, 0, 0);
echo "Created date is " . date("h:i:s", $d);
?>
See the documentation of mktime
It appears the main issue is the import CSV has a malformed date. Assuming you cannot change it and that the value is {hours}.{minutes}. Minutes not being a fraction but a value between 0 - 60. Otherwise, this will need adjusting to convert the fraction of an hour into minutes.
I'm using the DateTime class to achieve this.
$value = 9.1;
if (is_float($value)) {
$hour = (int)$value;
$minute = number_format(($value - floor($value)) * 100);
$date = new DateTime();
$date->setTime($hour, $minute);
} else {
$date = new DateTime($value);
}
$formattedTime = $date->format('H:i');
echo $formattedTime . "\n";
For 12 hours Formate
echo date("h:i:s a");
For 24 hours Formate
echo date("H:i:s");

strtotime subtraction not working

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

Get time passed always 0

This is what i am getting when i load the page
Code here:
echo "<b>Requested:</b><br>".round(abs(date("Y-m-d H:i:s") - date("Y-m-d H:i:s", strtotime($row['timestamp']))) / 60,2)." minute(s) ago<br>";
echo "<b>Current Time:</b><br>".date("Y-m-d H:i:s")."<br> ";
echo "<b>Requested Time:</b><br>".date("Y-m-d H:i:s", strtotime($row['timestamp']))."<br> ";
My problem is that it always says it was requested 0 minutes ago the image shows that it should be 7 minutes ago and counting . i have tried what the code in the link above shows ive tried messing with it with different formats and some give me huge numbers so i have come here for help
This should work for you:
<?php
//$row['timestamp'] = 1419272871; As an example
echo "<b>Requested:</b><br>".round(abs(strtotime(date("Y-m-d H:i:s")) - strtotime(date("Y-m-d H:i:s", $row['timestamp'])))/60, 2)." minute(s) ago<br>";
echo "<b>Current Time:</b><br>".date("Y-m-d H:i:s")."<br> ";
echo "<b>Requested Time:</b><br>".date("Y-m-d H:i:s", $row['timestamp'])."<br> ";
?>
Output:
Requested:
1686.53 minute(s) ago
Current Time:
2014-12-23 23:34:23
Requested Time:
2014-12-22 19:27:51
You are trying to do math with a string, since the date function creates a string in the format you supplied. Use datetime objects for this
$previousDate = $row['timestamp'];
$startdate = new DateTime($previousDate);
$endDate = new DateTime();
$interval = $endDate->diff($startdate);
$int = $interval->format('%i');
echo "<b>Requested:</b><br>" . $int ." minute(s) ago<br>";
This assumes that $row['timestamp'] is in the format 'YYYY-mm-dd HH:mm:ss' already. For a unix style timestamp, you want to use an ampersand in your construct
$previousDate = new DateTime('#' . $row['timestamp']);

Get 30 days back date along with time

I want to calculate EXACT past 30 days time period in php from now (for example 30 aug 14 23:06) to 30 days back (for example 1 aug 14 23:06). I wrote this where current datetime goes in $d1 and past 30 days datetime goes in $d2 but somehow i am not getting correct results. Any idea?
$url=$row["url"];
$pageid=getPageID($url);
$date=date('y-m-d g:i');
$d1=strtotime($date);
$d2=date(strtotime('today - 30 days'));
Thanks
The problem is likely caused by the malformed date() call. The first argument passed to date() should be the format (as shown in the Docs) and the second should be an optional timestamp.
Try this:
$d2 = date('c', strtotime('-30 days'));
PHPFiddle
As a short aside, the whole snippet can be simplified as follows:
$url = $row["url"];
$pageid = getPageID($url);
$date = date('y-m-d g:i');
$d1 = time();
$d2 = date('y-m-d g:i', strtotime('-30 days'));
You can also use the DateTime class's sub() method together with an DateInterval:
$now = new DateTime();
$back = $now->sub(DateInterval::createFromDateString('30 days'));
echo $back->format('y-m-d g:i');
if you would like to get out put as 2014-08-01 then try the below code. thanks
$date = '2014-08-30 23:06';
$new_date = date('Y-m-d G:i', strtotime($date.' - 29 days'));
echo "30 days back is " . $new_date;
From your brief description and example given, I believe that you want the date to be 30 days back and time to be the same as of now. The below code will serve this purpose. Thanks.
<?php
$date=date('y-m-d g:i');
$time=date('g:i');
echo "Todays date:" . $date. "<br>";
$d2 = date('y-m-d', strtotime('-30 days'));
echo "30 days back:" . $d2 . ' ' .$time;
?>
Try:
echo date("Y-m-d h:i:s",strtotime('-30 days'));
For more detail click here
Very simple two lines of code
$date = new DateTime();
echo $date->modify('-30 day')->format('y-m-d g:i');
I know you said with PHP, however, I can't imagine not getting the records from a DB. If you want to do so from the DB,use:
$sql='SELECT * FROM myTable WHERE date > CURRENT_DATE - INTERVAL 30 DAY';
$pdo->query($sql);
Very simple one lines of code:
echo (new DateTime())->modify('-30 day')->format('y-m-d g:i');
In the example below, it makes no sense if the variable $date is not
used anywhere else!
$date = new DateTime();
echo $date->modify('-30 day')->format('y-m-d g:i');
Sample answer is
$dateBack30Days=date('Y-m-d g:i', strtotime('-30 days'));

adding weekdays - clears the time

I've tired to add 10 weekdays to now. Everything is OK, but it clears the time part. Do you know why?
$now = date("Y-m-d H:i:s");
echo $now.'<br>';
$mod = strtotime($now." +10 weekdays");
echo $mod.'<br>';
echo date("Y-m-d H:i:s",$mod).'<br>';
Output:
2011-05-23 14:34:02
1307311200
2011-06-06 00:00:00
My expected output were:
2011-06-06 14:34:02
Thanks.
Looks like a difference in interpretation.
You could do the following to enforce the time:
<?php
$date = date("Y-m-d");
$time = date("H:i:s");
echo $date.' '.$time.'<br>';
$mod= strtotime($date." +10 weekdays $time");
echo $mod.'<br>';
echo date("Y-m-d H:i:s",$mod).'<br>';
There are several examples on the PHP documentation which can help you add days while still preserving the times.

Categories