how to convert 12h to 24h in php - php

I want to convert 12h time in php which is in this format: 05/31/2012 09:48 AM
to 24h time format : 2011-11-27 11:53:36
This is my code line that you can change:
$time= $_POST['time'];

You can use this:
// $time = 05/31/2012 09:48 AM
$time = $_POST['time'];
//output new time: 2012-05-31 09:48:00
$newTime = date("Y-m-d H:i:s", strtotime($time)) );
live example
Doc:
strtotime
date

http://psoug.org/snippet/Convert_12_to_24_hour_time_and_vice_versa_241.htm

You have to use strtotime like this:
$date="05/31/2012 09:48 AM";
echo date("Y-m-d H:i:s",strtotime($date));

go through below link for date functionality in PHP
http://php.net/manual/en/function.date.php

You can use strtotime and then date functions for that.
http://php.net/manual/en/function.date.php All the possible options are listed there.
The first argument to date is the format and the second is a timestamp (which you get with strtotime of the current date string you have).
date('Y-m-d H:i:s', strtotime($your_current_date_string));
The format is just off the top of my head and not exact. You can look up the format you need in the table in the link provided.

Related

wrong convert date() PHP

I used date() to convert 12H time to 24H using this code
$over = date("Y-m-d H:i:s", strtotime("2021-12-16 13:42:46 PM"));
echo $over;
but the output is this below:
1969-12-31 16:00:00
How to get rid of this, is this a bug? or my code?
sandbox
13:42:46 PM isn't 12h time format (PM is nonsense in 24h format), 01:42:46 PM is correct.
Just specify the correct date format (PM is not supported):
$over = date("Y-m-d H:i:s", strtotime("2021-12-16 13:42:46"));
echo $over;
Which date format is supported you can find in https://www.php.net/manual/de/datetime.formats.php

How do I convert this time format - 1480550400000+0000 in Y/m/d date format using php

I am trying to convert this time format - 1480550400000+0000 in Y/m/d date format using php date('Y/m/d',1480550400000+0000); but its not working. How can I make it work?
You timestamp has microseconds, so first remove it.
<?php
$timestamp = 1480550400000+0000;
$timestamp = intval($timestamp/1000);
$date = date("Y/m/d", $timestamp);
echo $date;
output: Check the live demo.
ei#localhost:~$ php test.php
2016/12/01
$dig_date= 1480550400000+0000;
$date = DateTime::createFromFormat('YmdGis', $dig_date);
echo $date->format('Y-m-d G:i:s');
Please note that the second parameter should be time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT). Looks like you have entered milliseconds instead.
PHP date()
string date ( string $format [, int $timestamp = time() ] )
timestamp
The optional timestamp parameter is an integer Unix timestamp that defaults to the current local time if a timestamp is not given. In other words, it defaults to the value of time().
Try this:
echo date('Y/m/d',1480550400+0000); // 2016/11/30
#Symplifys try this:
<?php
$date = date("Y-m-d", "1480550400000+0000");``
echo $date;
?>
Remember to put timestamp in double quotes.
Try this code at http://phpfiddle.org/
Solved I just divided this with 1000. $date = date("Y-m-d", $timestamp/1000); and it worked
Thanks

PHP - strftime (minutes and seconds ignored)?

I tried to save time in format YYYY-mm-dd 23:59:59 to mysql database column with datetime. I don't understand why minutes and seconds are ignored always 00 ? Thank you very much for help.
PHP:
$time = strftime('%Y-%m-%d %H:%i:%s', time());
Output:
2014-07-16 11:00:00
You can simple use:
$time = date("Y-m-d H:i:s");
or even better use mysql NOW() function
you have to specify timezone as follows.
date_default_timezone_set('Australia/Melbourne');
$date = date('m/d/Y h:i:s a', time());
echo $date;
The time would go by your server time. An easy workaround for this is to manually set the timezone by using date_default_timezone_set before the date() or time() functions.
you can use mysql function
now()
You are looking for
$time = strftime('%Y-%m-%d %H:%M:%S', time());
from the documentation
%S Two digit representation of the second
%M Two digit representation of the minute 00 through 59
Also, what PHP version are you using? %i:%s should not be 00:00

date format function

Maybe is a dumb question, but I can't find the proper function:
I have a string date like 04/05/2012
How I can pass to this format: 2012-04-05 20:18:11 to insert in mySQL?
Combination of date and strtotime ...
$my_date = date('Y-m-d H:i:s',strtotime('04/05/2012'));
$dateTime = new DateTime($your_time_string);
echo $dateTime->format("Y-m-d H:i:s");
Time can not be 'made' on its own. If you're however inserting the current date-time setting, then the following code will be used:
$dt = date( "Y-m-d H:i:s", time() );
You may use dleiftah's statement, but the time in that case will always be 00:00:00

PHP string to Date and Time

I have the following string: 2010-04-08T12:46:43+00:00
I want to convert that to:
8th April 2010 # 12:46
Is that easy enough?
Take a look at strtotime to create a UNIX timestamp from your time string, and then use date($format, $UNIXtimestamp); to create a normal date again:
$Timestamp = strtotime("2010-04-08T12:46:43+00:00");
echo date("your time format", $Timestamp);
You can look up the specific characters for the time format from PHP.net
Here you go.
EDIT : Exactly as you needed
Code:
$time_value = strtotime("2010-04-08T12:46:43+00:00");
$date_in_your_format = date( 'jS F Y # g:i', $time_value);
echo $date_in_your_format;
yep. use strtotime() + date()
Try taking a look at strtotime() and date().

Categories