Convert a date into a specific format using PHP [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I would like to convert a date that comes in the format 06.11.2017 um 18:30 - 21:30 Uhr into the format 2017-11-06. So everything should be cut off after 06.11.2017 and the remaining date should be output in the format YYYY-MM-DD.
Is there an elegant way to do that?

$date = "06.11.2017 um 18:30 - 21:30 Uhr";
$date = explode(" ",$date);
if(array_key_exists(0,$date))
echo date("Y-m-d", strtotime($date[0]));
output: 2017-11-06
Explode the string
Convert the first part to the desired format.

Not elegant but easy to implement :
function formatDate($date) {
$date = explode(' ', $date);
$date = explode('.', $date[0]);
return $date[2] .'-'. $date[1] .'-'. $date[0];
}
echo formatDate('06.11.2017 um 18:30 - 21:30 Uhr');

You can do it by using Explode function of PHP to cut the unwanted date and then change its format.
Below PHP code can help:
<?php
$date = "06.11.2017 um 18:30 - 21:30 Uhr";
$dateArr = explode(" ",$date);
$wantedDate = $dateArr[0];
$newDate = date("Y-m-d", strtotime($wantedDate));
echo $newDate;
// Output will be 2017-11-06
?>

Assuming your date string is always on that format, use substr() to cut the string then format the date to 'Y-m-d'
$date = '06.11.2017 um 18:30 - 21:30 Uhr';
$date = substr($date, 0, 10); // cut string to take date
$date = new DateTime($date); // set string as datetime object
echo $date->format('Y-m-d'); // formatted date (2017-11-06)

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.

convert date August 22, 2016 to 08/22/2016? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have this date format: August 22, 2016
I want to convert to 08/22/2016
str_replace is the solution?
eg: str_replace("August","08/",$var);
but it should have at least 12 str_replaces...
any ideas for a simple way?
Refer to DateTime::format
Specify your desired date format 'm/d/Y' after creating the DateTime object.
Try this:
$date = new DateTime('August 22, 2016');
echo $date->format('m/d/Y'); // 08/22/2016
try this,
$originalDate = "August 22, 2016";
$newDate = date("m/d/Y", strtotime($originalDate));
echo $newDate;
OUTPUT
08/22/2016
DEMO
just like php date function convert to in strtotime,
Example:
$date = "August 22, 2016";
$date2 = date("m/d/Y", strtotime($date));
echo $date2;
ANS : 08/22/2016
Note: if you change date format so change in date function so u like

How to convert date('H:i A d-M-Y') format in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I want to convert date format in php.
I have a variable $date = '05-27-2016 15:30'. I want to convert it into '3:30 PM 27-May-2016'.
I try this one date('H:i A d-M-Y',strtotime($date)) but it gives wrong result.
How do I do that?
Use createFromFormat function
$date = DateTime::createFromFormat('m-d-Y H:i', '05-27-2016 15:30');
echo $date->format('h:i A d-M-Y'); // 3:30 PM 27-May-2016
As i Commented out both the format, You need to change the format before changing final for final format.
If you need 3 at 03, just use g at h.
General: Online Check
$date = '27-05-2016 15:30';
echo date('h:i A d-M-Y', strtotime($date)); //03:30 PM 27-May-2016
Or ,
using Object Oriented: Online Check
$date = '05-27-2016 15:30';
$date = DateTime::createFromFormat('m-d-Y H:i', $date);
echo $date->format('h:i A d-M-Y');

PHP Convert date using strtotime? [duplicate]

This question already has answers here:
php strtotime not working
(3 answers)
Closed 7 years ago.
below is my php code.
<?php
$date = "06 06 2015 6:05:00 am";
$cdate = date("Y-m-d H:s:i",strtotime($date));
echo $cdate;
?>
Output is
1970-01-01 01:00:00
i need to 2015-06-06 6:05:00
Thanks
Try this
<?php
$date = "06 06 2015 6:05:00 am";
$datetime= \DateTime::createFromFormat("m d Y h:i:s a", $date);
$cdate = $datetime->format('Y-m-d H:s:i');
You want DateTime::createFromFormat() (http://php.net/manual/en/datetime.createfromformat.php). Using that you can convert a string to a datetime any way you want to.
If this is all you plan to do with dates, continue, otherwise, consider using a good date and time class like Carbon to do more with dates and generally save yourself a lot of effort.
$date = "06 06 2015 6:05:00 am";
$cdate = date_create_from_format('m d Y g:i:s a', $date);
This allows you to specify your own format using format strings. For the entire list of format strings see: http://php.net/manual/en/datetime.createfromformat.php
The resulting $cdate is a DateTime object and implements the DateTimeInterface (http://php.net/manual/en/class.datetimeinterface.php) so you can use the format() method to convert the output to any format you need.
For more on format(): http://php.net/manual/en/datetime.format.php
$date = "06 06 2015 6:05:00 am";
this format is not correct because first date or month but it will first year with the match of strtotime "Y-m-d H:s:i" and "06 06 2015" this is with space but it will be "-" sign.
$date = "2015-06-06 6:05:00 am";
$cdate = date("Y-m-d H:s:i",strtotime($date));
echo $cdate;

Convert date string to proper format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I am importing data from and excel sheet and the dates are formatted like so:
May 5, 2014
I need to convert this to a proper PHP date format like:
Y-m-d
This isn't working:
$new_date = date('Y-m-d', strtotime('May 5, 2014'));
echo $new_date;
Results
1969-12-31
I was thinking that i could do something like this:
$old_date = 'May 5, 2014';
$old_date = str_replace(',', '', $old_date);
$old_date_parts = explode(' ',$old_date);
// Convert month name to number
$may = '5';
// Build new date
$new_date = $old_date_parts[2].'-'.$may.'-'.$old_date_parts[1];
But there HAS to be another way!
Please help. Thank you in advance.
EDIT
Seems that the PHPExcel that i am using doesn't have the function to convert a serial date to a unix time so i used this.
UNIX_DATE = (EXCEL_DATE - 25569) * 86400
$date = date_create_from_format('M j, Y', 'May 5, 2014');
echo date_format($date, 'Y-m-d');
Or OOP Style
$date = DateTime::createFromFormat('M j, Y', 'May 5, 2014');
echo $date->format('Y-m-d');

Categories