Convert MySQL time to standard time with AM and PM - php

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.

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

PHP TIME converting?

I have something in my database that shows the time when the users who signed up on my recorded .. When the person makes the user saves the script (time ();) currently in the database under the variable name "REGTIM".
If I use echo to print it out, I get for example:
     1375202508
How can I make this a date if it is possible? or for example as mentioned below.
for example: 08/06/2013 2:11
You can use date() function with timestamp mention in the second argument,
date ( string $format [, int $timestamp = time() ] )
or in your case:
date('m/d/Y H:m', 1375202508);
you can read more on http://php.net/manual/en/function.date.php
You could have got the answer to that with a little bit of searching. But here you go:
echo date('m/d/Y h:m', 1375202508); // 07/30/2013 10:07 -- 12-hour format
echo date('m/d/Y H:m', 1375202508); // 07/30/2013 22:07 -- 24-hour format
See the documentation for more options: http://php.net/manual/en/function.date.php
that's probably just a unix timestamp, so use FROM_UNIXTIME() and DATE_FORMAT() to do the conversion/formatting inside your query, or use date() in PHP to work with the timestamp directly.
e.g.
echo date('d/m/Y h:m', $timestamp)
or
SELECT DATE_FORMAT('%d/%m/%y %h:%m', FROM_UNIXTIME(REGTIM))

Passing a UK date in a URL

I am passing a date in a URL in a UK format as per the following:
http://www.website.com/index.php?from_date=01/04/2013&to_date=12/04/2013
The date range is 1st April 2013 to 12th April 2013.
Then I am using the following PHP to convert it to the YYYY-MM-DD format.
<?php
$rpt_from_date = date("Y-m-d", strtotime($_GET["from_date"]) );
$rpt_to_date = date("Y-m-d", strtotime($_GET["to_date"]) );
echo $rpt_from_date;
echo "</br>";
echo $rpt_to_date;
?>
For some reason this is not working. The above returns the following:
2013-01-04
2013-12-04
It's switching the month and day around. I want it to return:
2013-04-01
2013-04-12
Does anyone have any ideas?
Use DateTime object, to get php understand in which format you passing date to it.
$rpt_from_date = DateTime::createFromFormat('d/m/Y', $_GET["from_date"]);
echo $rpt_from_date->format('Y-m-d');
PHP is reading your time string in the US format (MM/DD/YYYY) because you are using slashes. You could use dashes to give the time: index.php?from_date=01-04-2013&to_date=12-04-2013, or convert it yourself:
$uktime = implode("-", explode("/", $_GET['from_date']));
I will provide the solution but it is not using the date function:
$arr = explode("/",$_GET["from_date"]);
$from_date = $arr[2]."-".$arr[1]."-"$arr[0];
Second solution is as following:
$from_date = implode(array_reverse(explode("/",$_GET["from_date"])));

how can i get which month a given year day is in in php?

I have been given year day (1-366) and I need to figure out which month it is in, how can I do this?
Well, I actually have a date string like : year, day or year, minute of day, second and I ultimately want to create a POSIX timestamp from it, how can I do this?
Thank you!
If you have PHP >= 5.3, then you can use DateTime::createFromFormat.
$day = 176;
$date = DateTime::createFromFormat('z', $day);
echo $date->getTimestamp(); // 1372275280
<?php
$year=2013;
$d=360;
echo date("m",strtotime("1/1/$year + $d days"))
?>
Use the date function to get a posix time stamp.
To get the month of a certain date, use intval(date('m'), mktime($h,$m,$s,$month,$day,$year))

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

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

Categories