I am getting a datetime format from a form in this format
// $start equals 2014-04-19 12:00:am
// I need to convert to 24hr 2014-04-19 00:00:00
$startconvert = str_replace(':am', '', $start);
$startconverted = date( 'Y-d-m H:i:s', strtotime($startconvert));
echo($startconverted);
//becomes 2014-19-04 12:00:00 which is 12pm
Anyone know how to do this? This looks right to me but it ends up being 12pm
$date = DateTime::createFromFormat('Y-m-d h:i:a', '2014-04-19 12:00:am');
echo $date->format('Y-m-d H:i:s');
This just takes the date you have and reads it in by its parts using DateTime::createFromFormat(). That DateTime object can then be formatted normally.
Related
I want to check the date input and convert it to 24h format. For example the datetime is:
$date="2021-02-5 11:45:00 AM"
and after i check it with this condition:
if (preg_match("/am|pm|AM|PM/", $date))
{
$date=date('Y-m-d H:i:s',strtotime("$date"));
}
and my output should be like this however it does not return a date format like that:
$date="2021-02-5 11:45:00";
How can i fix this problem?
Converting time format
// 24-hour time to 12-hour time
$time_in_12_hour_format = date("g:i a", strtotime("13:30"));
// 12-hour time to 24-hour time
$time_in_24_hour_format = date("H:i", strtotime("1:30 PM"));
You don't need regex to check if it is 24 hours or 12 hours. you can simply pass the date to strtotime function to get a timestamp of the date and then format the timestamp with date function.
$date="2021-02-5 11:45:00 AM";
$date=date('Y-m-j H:i:s',strtotime($date));
echo $date; // 2021-02-5 11:45:00
date_create_from_format('Y-m-j H:i:s A', $date);
It will return a DateTime object, and you can get right date you want, and it will return false if date string don't match the pattern.
DateTime Object
(
[date] => 2021-02-05 11:45:00.000000
[timezone_type] => 3
[timezone] => UTC
)
date_create_from_format ( string $format , string $time , DateTimeZone $timezone = ? ) : DateTime
With strtotime you have no control over the formats. To be on top of what is happening better use the feature-rich DateTime class. First convert the string to a DateTime object and then format it to whatever you need.
<?php
$dt = "2021-02-5 11:45:00 pm";
if (preg_match('/am$|pm$/i', $dt)) // is there an Ante meridiem/Post meridiem suffix?
{
$dtFormat = 'Y-n-j H:i:s a';
}
else
{
$dtFormat = 'Y-n-j H:i:s';
}
$ts = DateTime::createFromFormat($dtFormat, $dt);
$date = $ts -> format('Y-m-d H:i:s');
// echo 'Result: '. $date;
I am working on my php script to set up the date with the time. I need some help with convert the day date to the current day and next day date, example: my current time is 15:27 and my current date is 27-11-2019 so when I have the string for the variable get_time1 is 06:00:00, I want to convert it to 28-11-2019 06:00:00. When I have the variable get_time2 that have the time which it is 23:00:00 as my current time is before 23:00:00 so i want to convert the date with the current date with the time to 27-11-2019 23:00:00.
Code:
<?php
$get_time1 = '06:00:00';
$get_time2 = '23:00:00';
date_default_timezone_set('Europe/London');
$Date = date('Y-m-d');
$time = date('H:i:s');
?>
Can you please show me an example how I can set up the day date with the time 06:00:00 and 23:00:00 as if the time 06:00:00 is after 12am to set up the next day date and if the time 23:00:00 is before 12am then set up the time with the current date?
Thank you.
This just creates a DateTime object from the time (which will default it to todays date) and if this is less than the current date and time, it adds 1 day...
$date = DateTime::createFromFormat("H:i:s", $get_time2);
if ( $date < new DateTime() ) {
$date->modify("+1 day");
}
which gives
2019-11-27 23:00:00
and for $get_time1...
2019-11-28 06:00:00
If you use a DateTime that will allow you to do date arithmetic.
$now = new DateTime();
$tomorrow = $now->modify("+1 day");
You can also use strtotime to get a unix timestamp as explained in this answer.
$tomorrow = strtotime('+1 day');
maybe this will do?
$offset = timezone_offset_get( timezone_open( "Europe/London" ), new \DateTime() );
echo 'in London' . gmdate('d-m-Y H:i:s', date( "U" )+$offset);
echo 'current location: ' . date('d-m-Y H:i:s', date( "U" ));
I'm trying to convert 01-31-2017 09:01 AM into 24 hour datetime ( i have AM PM in my values), but it keep giving me 1969-12-31 16:00:00
Here's what I've done:
$old_date = strtotime("01-31-2017 09:01 AM");
$new_date = date('Y-m-d H:i:s', $old_date);
Any kind of help I can get on this is greatly appreciated!
If you know the format of the string you better use the date_create_from_format function:
$s = '01-31-2017 09:01 AM';
$date = date_create_from_format('m-d-Y h:i A', $s);
var_dump($date->format('Y-m-d H:i:s'));
When you use the strtotime you let the php parse the string, and it might lead to a result you are not looking for (for example - 01-02-2017 - is it jan 2nd or feb 1st?).
I am trying to format a date as 2015-07-12 15:00 from the values declared in my variables
// unix
$date = 1436713200
// string
$time = '15:00';
to get a date format 2015-07-12 15:00 but failing, using this
$newdate = date('Y-m-d H:i:s', $date.' '.$time);
I get 'A non well formed numeric value encountered'. Can anyone help? I understand it is possibly due to the mix of string and unix but unsure how to get round this.
I would suggest you to use DateTime instance to avoid timezone issues:
$d = date_create('#1436713200'); // creates DateTime instance
$d->setTime(15, 00); // sets current time to desired hours, minutes
echo $d->format('Y-m-d H:i:s'); // prints it out with format specified
//⇒ 2015-07-12 15:00:00
You do not have to provie the $time variable. Unix time is a full date with time.
Use:
$newdate = date('Y-m-d H:i:s', $date);
Use this
$date = date('Y-m-d','1436713200');
// string
$time = '15:00';
echo $newdate = date('Y-m-d H:i', strtotime($date.' '.$time));
Given a date string formatted Y-m-d (2014-4-11 for example), how can I get a UNIX timestamp of 12am the beginning of that day? Would it involve date_parse_from_format()?
Thanks
You can simply use strtotime()
$date = "2014-04-11";
$timestamp = strtotime($date);
which inturm gives you -
$d = date('Y-m-d H:i:s', $timestamp); // 2014-04-11 00:00:00
Try online conversion to test - http://www.onlineconversion.com/unix_time.htm