I currently get duration in this format: 04:00 but I need in this format 00:04:00.
I am using this code but it's not working correctly.
$time = date("h:i:s", strtotime($duration));
echo $time;
try like this,
$duration = "00:"."04:00";
This should work for you:
(Your problem is in which format you read in your time, so DateTime::createFromFormat)
$date = DateTime::createFromFormat("i:s", "04:00");
echo $date->format('H:i:s');
Output:
00:04:00
The problem is the strtotime($duration); It doesn't know exactly what format that's in, and can't get a valid date from it. (This is why you're getting unexpected results from the date function.
If you want to just simply add 00: before $duration, you can do it like so:
$duration = '04:00';
$duration = '00:' . $duration;
You then would be able to pass this to the strtotime function, and would likely get better results.
Related
I am trying to add 1 hour to a timestamp field fetched from database using the following code.
date($ls['created_at'], strtotime('+1 hour'));
However, this doesn't seem to work. It returns the same time as in database. Am I missing something? Or, is the code deprecated? What is the proper solution?
You need to give it the correct syntax to use this,
You need to send the time to change with the change itself in the function - for example (using date for wanted format):
$date = "22-02-2021 14:22:22";
echo date("d-m-Y H:i:s", strtotime($date.' +1 hour'));
This will return:
22-02-2021 15:22:22
Same as this:
echo date("d-m-Y H:i:s", strtotime("22-02-2021 14:22:22 + 1 hour"));
The idea is that you strtotime receives the date and data to change in one string like this :
echo strtotime("22-02-2021 14:22:22 + 2 hour");
Will return:
1614010942
Here I removed the Date Format so I received a unix timestamp format
I want to know how to pick the time format from datetime in yii2.
So if there is code like this
$model->date = date('Y-m-d H:i:s', strtotime('+5 hours'));
and the result would be 2018-05-08 23:36:21
how can I extract the time only? so the result is only 23:36:21
I already tried using code below
date("H:i:s", strtotime('-30 minutes'));
but I only got like 00:30:00
Is there something wrong with the code?
Any help would be appreciated :)
if you have the string 2018-05-08 23:36:21 and want to extract time, you can follow the following methods
Using php:date() function
Remove -30m from the time when you format the date
echo date('H:i:s',strtotime('2018-05-08 23:36:21 -30 minutes'));
Using DateTime Object
The method sub() can subtract from the time and output the remaining time using $dateTimeObj->format().
$date1=new \DateTime('2018-05-08 23:36:21');
$date1->sub(new \DateInterval('PT30M'));
echo $date1->format('H:i:s');
I have value 10:00:00 in my mor_start which is of type time in mysql database i want it to increment by 15 minutes.
$tim=$values['mor_start'];
$date = date('H:i:s', strtotime($tim));
echo $date;
the above code displays 10:00:00
but when i try to increment the time by the following code i get error : Notice: A non well formed numeric value encountered
$tim=$values['mor_start'];
$date = date('H:i:s', strtotime("+15 minutes",$tim));
echo $date;
As Mark Baker said (he should deserve the answer) one correct solution is replace the calculation by this :
strtotime("+15 minutes",strtotime($tim)));
Calling strtotime() multiple times is a little overkill. Code with DateTime extension is a faster example (and ofc more beautiful), like this demo:
echo date_create('10:00:00')->modify('+15 minute')->format('H:i:s');
Test this demo on your localhost and see the difference. My result on 1e6 loops is:
add15min_v1() has looped 1000000x times in 23.47sec.
add15min_v2() has looped 1000000x times in 6.81sec.
try this
$date = date('H:i:s', strtotime("+15 minutes", strtotime($tim)));
http://php.net/manual/en/function.strtotime.php
$tim=$values['mor_start'];
$date = date("H:i:s", strtotime("+15 minutes",strtotime($tim)));
echo $date;
This should work.
Hi guys i am really new to php and i am trying to convert the timestamp from an xml array but with no sucess , i read everything i found but still can find the way can you please help ?
I am using this code to decode an xml api output
$mysongs = simplexml_load_file('http://example.com/test/xml/');
$timestamp = $mysongs->item->timestamp;
$playtime = date("Y-d-m G-i-s",$timestamp);
If i echo $timestamp it works fine, $playtime doesn't...
Tried with :
echo gmdate("Y-m-d", $timestamp);
&
echo date('Y-m-d H:i:s', $wra);
&
echo '<timeplayed>' . date('Y-m-d G:i:s', $mysongs->item->timestamp) . '</timeplayed>';
Still no luck.. Time is not showing.. If i use this example
echo '<timeplayed>' . date('Y-m-d G:i:s', (1365532902)) . '</timeplayed>';
it works fine.. What am i doing wrong here ?
Update
Finally i found it.. it needed to cast the $timestamp as integer for the date to decode properly as euxneks sugested..
So right code should be
$timestamp = intVal ($mysongs->item->timestamp);
and then
$playtime = date("Y-d-m H-i-s",($timestamp));
& finally echo ($playtime); and it works fine...
Thanks everyone for your replys problem solved
It's likely that you need to cast your result as an integer (it's been a while since I've used simplexml but I'm pretty sure it doesn't automatically type the values received):
http://php.net/intval
Also, strtotime might work too :)
http://php.net/strtotime
You are definitely missing strtotime function, here is an example:
$str = '04/09/2013';
$date = date('Y-m-d H:i:s',strtotime($str));
echo $date;
This will output:
2013-04-09 00:00:00
Updated
Since strtotime is alread used, what about using:
$date = date('Y-m-d H:i:s', ($timestamp));
So this should be a real easy question but I can't seem to find a simple answer anywhere.
I'm patching up some PHP code (I'm not a PHP'er) and I have this variable $orderDate. How do I print this variable so that its just M/d/yy h:mm tt?
Update:
So I looked around and saw what $orderDate is. Here's the code:
global $orderDate;
$orderDate = strftime('%c');
print("Order Date: ".date("M/d/Y h:M", $orderdate)."<br />");
so I get this for output:
Dec/31/1969 06:Dec
and should be getting today's date....
echo date("m/d/Y h:m", $orderDate);
echo date("m/d/Y h:m", strtotime($orderDate)); // or this
Depends on what $orderDate contains.
Look into date() since it has there plenty of examples and is pretty simple to use.
UPDATE:
$orderDate = date("M/d/Y h:M");
print("Order Date: ".orderDate ."<br />");
Also check out to see if this works for you.
date function will do that for you.
If $orderDate is an integer time stamp, you probably want strftime. Specifically, I think the call you want would be:
strftime("%D %l:%M %p", $orderDate)
However, I recommend reviewing the web page to make sure I've interpreted what you want correctly.
See the PHP date() function.
Returns a string formatted according to the given format string using the given integer timestamp or the current time if no timestamp is given.
string date ( string $format [, int $timestamp = time() ] )