how to convert hour minute and pm/m to strtotime - php

I have a time value 04:30 (h:mts am/pm) pm. I want to convert this to strtotime
Also I need to convert back from strtotime to this format (h:mits am/pm)
Thanks

Try this:
$time_string = '04:30 pm';
$time = strtotime($time_string);
echo 'PHP Time: ' . $time . PHP_EOL;
echo 'Human Time: ' . date('h:i a', $time);
Demo here: http://ideone.com/SR58g

read this documentation of php http://php.net/manual/en/function.time.php

Related

Convert time from AM/PM to 24 hour [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a time as 5:4 pm and i want convert the time from AM/PM to 24 hour format in PHP, i try as date('H:i',strtotime('5:4 pm')) but this don't work and result is 16:00 in the event that it should be 17:04. what do i do?
DEMO: http://sandbox.onlinephpfunctions.com/code/bfd524f65ea4fa3031e55c9879aab711f31e1b37
I can not change this time.
I recommend PHP OOP way as they are always better than any procedural way(like using strtotime):
$time = '5:04 pm';
$date = DateTime::createFromFormat('g:i a', $time);
echo $date->format('H:i');//17:04
Please mind that you need to provide : 5:04 pm , you CAN NOT use 5:4 pm .
Reason is that no date format exist for minutes without a leading zero.
For reference see this:
http://php.net/manual/en/datetime.createfromformat.php
If you have to have time in that format then you will need to manipulate it after you receive your time as follows:
$time = '5:4 pm';//works for formats -> '5:4 pm' gives 17:04,'5:40 pm' gives 17:40
$time2 = str_replace(' ',':',$time);
$time3 = explode(':',$time2);
if(((int)$time3[1])<10)//check if minutes as over 10 or under 10 and change $time accordingly
$time = $time3[0].':0'.$time3[1].' '.$time3[2];
else
$time = $time3[0].':'.$time3[1].' '.$time3[2];
$date = DateTime::createFromFormat('g:i a', $time);
echo $date->format('H:i');
I hope it helps
Assuming the time string format will be same -
$time = explode(' ', '5:4 pm');
$temp = date_parse($time[0]);
$temp['minute'] = str_pad($temp['minute'], 2, '0', STR_PAD_LEFT);
echo date('H:i a', strtotime($temp['hour'] . ':' . $temp['minute'] . ' ' . $time[1]));
Output
17:04 pm
You should pass 5:04 pm instead of 5:4 pm, it seems the parameter expects 2 digits for the minute format.
Working example: http://sandbox.onlinephpfunctions.com/code/ecf968c844da50e8a9eec2dc85656f49d7d20dee
You can try this
echo date("H:i", strtotime("05:04 PM"));
Try to capitalize AM/PM
date('H:i', strtotime('5:04 PM'))
{EDIT}
Replace pm with PM using str_replace
echo date('H:i', strtotime(str_replace("pm","PM",'5:04 PM')))
Here is the code,
$time = '5:4 pm';
$arr = explode(" ",$time);
$arr1 = explode(":",$arr[0]);
foreach($arr1 as $k => $val){
$arr1[$k] = sprintf("%02d", $val);
}
$str = implode(":", $arr1)." ".$arr[1];
echo date('H:i',strtotime($str));
pure date wont work, so I am exploding it as it is special case
I hope this will work.
Thanks :)
// 24 hours format
$date = date("H:i a",time());
// just pass time() function instead of strtotime()
echo $date;
// output
// 17:04 pm
// to run a test, do reset your system time back or forward to 5:4 pm
Thanks, hope this helps.

Echoing time from now and one hour later

Here I have set the my Centos time Zone.> sudo hwclock – show
Tue 04 Feb 2014 10:23:10 AM AFT -0.596389 seconds
asia/kabul
Now on PHP I want to echo from 10:23:10 AM to 11:23:10 AM
Here I have set my code for the echoing.
<? echo (date('G', time())+5) ;
echo(':00 To ');
echo (date('G', time())+6);
echo(':00'); ?>
Now as result of my above echoing PHP code I get the result of
15:00 To 16:00
But instead I want to get the echo of below or as what ever my HTTP server time is from NOW to 1 hour next.
10:23:10 AM to 11:23:10 AM
You should try strtotime(). Something like this:
echo date('h:i:s A'), ' to ', date('h:i:s A', strtotime('+1 hour'));
echo date('Y-m-d H:i:s', strtotime('now')), ' to ', date('Y-m-d H:i:s', strtotime('+1 hour'));
For date format, please read: http://www.php.net/manual/en/function.date.php
For your format:
echo date('h:i:s A', strtotime('now')), ' to ', date('h:i:s A', strtotime('+1 hour'));
Add to the time and then print the date.
echo date('G',time()+3600);
time() returns number of seconds since the unix epoch, then you add 3600 seconds for 1 hour and you use that in your date.
You can use explode function.
<?PHP
$time = date('G:i:s');
$eTime = explode(':', $time);
$timePlusOneHour = $eTime+1 . ":{$eTime[1]}:{$eTime[2]}";
echo $time . " To " . $timePlusOneHour;

need help converting time string to usable format

I have a string in the following format
yyyy-mm-dd-hh-mm-ss
The reason its in this format is because its part of a URL. I need help converting it to a usable time format in PHP.
Here is what I have tried so far.
$time = "2013-04-14-23-33-17";
$time2 = strtotime($time);
$time3 = date('M d, Y', $time2) . ' ' . _('at') . ' ' . date('h:i a', $time2);
echo $time3;
It just echos out Jan 01, 1970 at 12:00 am which is wrong instead of Apr 14, 2013 at 11:33 pm.
It doesn't work because that's not a valid date format. See Supported Date and Time Formats.
What you need to do is create a DateTime object by specifing a format for your string.
DateTime::createFromFormat does exactly that.
You could use DateTime to help you with the parsing:
$date = DateTime::createFromFormat('Y-m-d-H-i-s', $time);
echo $date->format('M d, Y h:i a');

unix timestamp round to midnight

If I have a random unix timestamp, how can I round it down to today's midnight or the midnight selected by the user. The reason for this is that I want to add hours and minutes after a certain day's midnight.
For example if the timestamp is 1324189035 then how can I remove the hours, minutes, and seconds to put the timestamp at midnight for that day.
echo date('d-m-Y H:i:s', strtotime('today', 1324189035));
Because of how you're using it, I wouldn't calculate midnight at all: it is far easier to simply convert what you're adding to the timestamp into 24 hour time and then use strtotime:
echo strtotime("0:00",1324189035); // 1324184400
echo strtotime("17:50",1324189035); // 1324248600
And if you want to have that in human readable, use date and m/d/Y H:i:s:
echo date('m/d/Y H:i:s', strtotime('17:50',1324189035)); // 12/18/2011 17:50:00
Simply Use
strtotime('today midnight');
Just do
date('d-m-Y',strtotime('today'));
Easy!
An easy solution would be to use the modulo expression to remove the exceeded seconds from a round day timestamp.
<?php
date_default_timezone_set('UTC');
$timestamp = time();
echo "timestamp : " . $timestamp . PHP_EOL;
echo "timestamp formatted : " . date('Y-m-d H:i:s', $timestamp) . PHP_EOL;
$diff = $timestamp % (60 * 60 * 24);
echo "diff : " . $diff . PHP_EOL;
$midnight = $timestamp - $diff;
echo "midnight : " . $midnight . PHP_EOL;
echo "midnight formatted : " . date('Y-m-d H:i:s', $midnight) . PHP_EOL;
This would output the following result.
timestamp : 1575451074
timestamp formatted : 2019-12-04 09:17:54
diff midnight : 1575417600
midnight formatted : 2019-12-04 00:00:00
And here is a one liner function to get your midnight from any timestamp.
function getMidnight ($timestamp) { return $timestamp - ($timestamp % 86400); }
http://sandbox.onlinephpfunctions.com/code/1d85c935e71fcf011284ae33658e0c68dd8d8c28
How about just:
date -d $(date +%F) +%s

How to increment a MySQL timestamp in PHP?

So I have a MYSQL timestampl that is generating values that are 4hours ahead of what they should be. How would i subtract 4hrs from the MySQL time stamp and display that?
Here's my current code
$mdate = date('F j, Y, g:i a',strtotime( $bm->date_added));
//$bm->dated_added returns the times stamp//
echo $mdate;
Thanks
When supplied with a second argument, strtotime() will perform calculations based on this date:
$mdate = date('F j, Y, g:i a', strtotime('-4 hours', $bm->date_added));
strtotime() can perform several calculations base on modifiers like these. Examples from the manual:
<?php
echo strtotime("now"), "\n";
echo strtotime("10 September 2000"), "\n";
echo strtotime("+1 day"), "\n";
echo strtotime("+1 week"), "\n";
echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";
echo strtotime("next Thursday"), "\n";
echo strtotime("last Monday"), "\n";
?>
You should really figure out the root problem and fix it. Most likely you need to set MySQL's timezone to fit your own (or the server itself has the wrong date set).
That said, you can just subtract 14,400 seconds from the strtotime result.
$mdate = date('F j, Y, g:i a',strtotime($bm->date_added) - 14400);
Use the SUBTIME() function in your query if you cannot change the server time to reflect a appropriate timestamp
SQL approach:
SELECT TIMESTAMPADD(the_timestamp, INTERVAL -4 HOURS)
This will return you the offset time

Categories