Convert PHP variable "11:00 AM" to MySQL time format - php

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

Related

Short time str not displaying correctly

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

How to update certain values of a php date string?

I have some date string values that I want to be able to update for checking against in some conditional statements. I want to update the hour, minute and seconds values to be at 23:59:59.
Say I have the variable $value which prints to
2017-03-08 00:00:00
How can I update the value to be
2017-03-08 23:59:59
?
Use DateTime.
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s',$value);
$dateTime->setTime(23,59,59);
$value = $dateTime->format('Y-m-d H:i:s');
http://php.net/manual/en/class.datetime.php
You could do many more things with the DateTime class.
Hi there this is the code below which you can use to update the time value:
your variable in php:
$value = "2017-03-08 00:00:00";
the html tag like so:
<input type="datetime-local" name="date" value="<?php echo date("Y-m-d\TH:i:s", strtotime($value)); ?>" />
This tag also generates a date and time picker, assuming you a running a compatible browser.
Note: In the future please show us your work and what you have achieved because you cannot expect someone to do it all for you, good luck!
In this simple case, you could probably leverage strtotime to get what you want:
$myDate = '2017-03-08 00:00:00';
$myTime = strtotime($myDate) + 86399;
echo date('Y-m-d H:i:s', $myTime);
Though in more difficult cases it would probably be better to use PHP's DateTime class:
$myDate = '2017-03-08 00:00:00';
$dt = new DateTime($myDate);
// Subtract one second
$dt->add(new DateInterval('PT86399S'));
// Output formatted result
echo $dt->format('Y-m-d H:i:s');
You could also look into Carbon for handling all of your date / time needs.

Add 5 minutes difference

I have a problem.
I have a certain date/time (A), which is put in a variable. Lets say its May 06 2014 14:26.
I want to compare this date/time with file's modification date/time (B), and if it is the same, I put the file's modification date/time (B) in variable too.
The thing is, I cant get exactly the same date on file (B). So, I want that it would add 5 minutes difference. For example, if file (B) has modification date May 06 2014 14:28, it will be put in a variable. Or if it has modification date May 06 2014 14:22, it will be put in a variable too.
If it helps, I am using date("F d Y H:i",$file['filetime']) command to get the modification date of the file.
Any possibility to do this? Thanks in advance.
If something is unclear, just tell me. I'm new here, so might be hard to explain things correctly.
Maybe, it helps:
$datetimeA = strtotime($file1['filetime']);
$datetimeB = strtotime($file2['filetime']);
$interval = abs($datetimeA - $datetimeB);
$minutes = round($interval / 60);
if ($minutes > 5) {
//do some magic here
}
If $file['filetime'] is a timestamp already, then You can use it without calling strtotime().
With the DateTime class, it is done in a few lines :
$datetime1 = new DateTime('2009-10-13 10:00:00');
$datetime2 = new DateTime('2009-10-13 10:20:00');
$interval = $datetime1->diff($datetime2);
$yourIntervalle = $interval->format('%i minutes'));
i'd either directly use unix-timestamps or convert A and B to Unix Timestamps (e.g. using strtotime) and compare them based on your requirements.
if you have access to the file directly you could even use filemtime to directly get the last modified time as an unix timestamp.
Guess this is what you're looking for, if your problem is just to add 5 minutes:
$dateB = new DateTime('2014-05-06 14:26:00');
$interval = new DateInterval('PT5M');
$dateB->add($interval);
print_r($dateB);
PHPSandbox: http://sandbox.onlinephpfunctions.com/code/9f9a2ee40f09f953c796c9c2dd7e15ad62b45772
Also, look at DateInterval to understand a little more:
http://www.php.net/manual/en/dateinterval.construct.php
you can do like. if you want to minus 5 minutes from the date
$given_date = 'May 06 2014 14:28';
$date = new DateTime($given_date);
//subtract 5 minutes from the date
$new_date = $date->sub(new DateInterval('PT' . '5' . 'M'));
echo $new_date->format('F d Y H:i');
if you want to add 5 minutes then change
//add 5 minutes to the date
$new_date = $date->add(new DateInterval('PT' . '5' . 'M'));
First of all, i think you need to compare the two date/time by the method strtotime. With that you can compare the two date.
Then, you just need to make one or two insert in your database if i understand correctly with a If/Else method.
Hope it helps!

subtract time away from a php date time

I have a report I built but the problem is the datetimes in the database for the 3 major events are the same as the system processes then so fast, there is no easy way about it as I aggregate data from 4 servers into one jquery datatable and sort by date time decending.
So my question is how can I take a variable in PHP (string of mysql format date time), and reduce it by 1 second?
dognose answer is fine. Find below a method using DateTime.
For those who are not too confident about strtotime :-)
$string = "2013-06-26 18:00:00";
$date = DateTime::createFromFormat('Y-m-d H:i:s', $string);
$date->sub(new DateInterval('PT1S'));//substract 1 sec
echo $date->format('Y-m-d H:i:s'); //print : 2013-06-26 17:59:59
Doc about "PT1S" here (this can be read as Period Time 1 second)
use date along with strtotime should do the trick:
http://php.net/manual/de/function.strtotime.php
$string = "2013-06-26 18:00:00"; //can have any (valid) format
$subSeconds = 1;
$date = date("Y-m-d H:i:s", strtotime($string . " - {$subSeconds} second"));
echo $date."<br />"; //2013-06-26 17:59:59

Convert MySQL time to standard time with AM and PM

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.

Categories