Iam having variable with value 9:30 am - 12:00 pm i.e
$val=$shift (o/p 9:30 am - 12:00pm)
I want to convert it to 09:30 am - 12:00pm, i mean i need to add 0 infront of number if time having one digit
If you are using date function then you can format time using h.
For example
echo date('h:ia'); // output 09:50am
See Referance
If you have values from DataBase as you mentioned in comment then you need to apply PHP's built-in function strtotime() before formatting the time.
For Example
echo date( 'h:ia', strtotime('9:30 am') ); // output 09:50am
Assuming you are saving your time in your database as a string as 9:30 am - 12:00pm you can just split them. Then format each data.
$val= "9:30 am - 12:00pm";
$splittedString = explode('-', $val);
$time1 = date('h:ia',strtotime($splittedString[0]));
$time2 = date('h:ia',strtotime($splittedString[1]));
echo $time1." - ".$time2;
Try
echo date('h:ia',strtotime("9:30 am"))." - ".date('h:ia',strtotime("12:00pm"));
Related
lets assume that NOW is 2021-09-15 (DBDatetime format). I would like to add to that existing date -1 day and set time to be at 10:00 AM, so:
Initial date would be: 2021-09-15
Expired date would be: 2021-09-16 10:00 AM
The reason for that is I would like to get videos (on my newbie site) even after their airing time exired. So lets say expiring time is set to initial date and i want to have that till 21-09-16 10:00 AM is up.
Is that possible?
Yes, it's very easy with DateTime::modify.
$basisDate = '2021-09-15'; //or 'today' for the current day
$date = date_create($basisDate)->modify('+1 Day 10:00');
//test output
echo $date->format("Y-m-d H:i"); //2021-09-16 10:00
Another variant, not so tricky and easier to understand:
$date = date_create($basisDate)
->modify('+1 Day')
->setTime(10,0)
;
I'm using the following function to display time:
date('g:i A', strtotime($time))
If $time is a 4 digit integer, like 1430 the time shows correctly as 2:30 PM. However if the if it's before 10am, say 8am, the time string is 800 and the function shows it as 12:00 am.
I cannot edit time value in my db. How can I fix it on a PHP side?
Thanks.
You need to pad the integer value you are storing in $time with zeroes
Use either of the below functions :
$formatted_time = sprintf("%04d", $time);
OR
$formatted_time = str_pad($time, 4, '0', STR_PAD_LEFT);
Use this value as input to the strtotime function.
Pop an instance of DateTime() and define your timezone and set a timestamp
$date->setTimezone(new DateTimeZone("Asia/Tokyo"))->setTimestamp(800);
3v4l.org/iLk57
i would to know how can i parse date and time which we choose from date time picker into standard date('YmdHis') type of results?
$datetime = date('YmdHis);
eg date picker return result 11/13/2012 and time picker result is 2.30 AM, now i need to know how to joining them together and convert it to 20121113023000 format?
echo date("YmdHis",strtotime('11/13/2012 2.30 AM'));
or
$data = $_POST['date_picker']." ".$_POST['time_picker'];
echo date("YmdHis",strtotime($data));
this will output that in required format
outputs
20121113023000
This should work
$datatime = date('YmdHis',strtotime($_POST['datepickerfield']));
echo date("Y-m-d H:i:s",strtotime("11/13/2012 2.30 AM"));
outputs
2012-11-13 02:30:00
Trying to convert standard time variable from form input to TIME format acceptable for MySQL INSERT. I might be going about it all wrong and could use some help. I've read through the MySQL TIME functions and PHP TIME functions but haven't found a solution yet.
Here's what I've tried as an example:
<?php
$time_input = '11:00 AM';
$strtotime = strtotime($time_input);
$mysql_time = date('H:m:s',$strtotime);
echo "<p>time_input: $time_input</p>";
echo "<p>strtotime: $strtotime</p>";
echo "<p>mysql_time: $mysql_time</p>";
?>
The result is changing the time to 11:08:00 (not sure where the 8 minutes is coming from):
time_input: 11:00 AM
strtotime: 1344438000
mysql_time: 11:08:00
Any help is much appreciated!
DateTime will do this for you:
$time_input = '11:00 AM';
$date = DateTime::createFromFormat( 'H:i A', $time_input);
$formatted = $date->format( 'H:i:s');
I typically avoid strtotime() when possible as it can be somewhat unpredictable.
You can see it work in this demo.
m is the format for the month with leading zeros (August, i.e. 08), you want i (minutes with leading zeros):
$mysql_time = date('H:i:s',$strtotime);
PHP date docs
Having trouble converting AM time values from MySQL fetch_array when using the PHP date() function. Not sure why the stop time below is being printed as the 7:00PM when it should be 11:00AM. Any ideas? Any help is much appreciated!
<?php
$mysql_output = array('start' => '19:00:00','stop' => '11:00:00');
$start_time = date('g:iA',$mysql_output['start']);
$stop_time = date('g:iA',$mysql_output['stop']);
echo "<p>start_time: $start_time</p>";
echo "<p>stop_time: $stop_time</p>";
?>
The result doesn't convert the AM time correctly; should read 11:00AM for stop_time:
start_time: 7:00PM
stop_time: 7:00PM
The date() function takes a Unix timestamp, not a string datetimestamp. You need to put a MySQL datetimestamp through strtotime() first.
$start_time = date('g:iA', strtotime($mysql_output['start']));
$stop_time = date('g:iA', strtotime($mysql_output['stop']));
If you want mysql to convert it for you, just use the DATE_FORMAT( ) function.
SQLFiddle Demo
The date function is misinterpreting your data.
You should create a time object first, then pass that in as a parameter to date.