String to datatime [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Convert this string to datetime
I have to convert my string value into datetime.Is there any php function which can convert varchar value into datatime.my value are coming like this:
$a= "2013-Jan-12-(Sat) 03:30 PM";
and i want to convert it simple datetime. Like: 2013-1-12 15:30:45

$Date = new DateTime("13-Jan-12-(Sat) 03:30 PM")
should do the trick. if thats not working, look at strtotime.

May be the sat is the problem try to change that or add that at first, you may get it.

Related

Convert date April/2019 to 04/2019 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
Now I'm having a date like this, April/2019. I wanted to convert it into 04/2019 in php. How to resolve this?
date('m/Y', strtotime($_POST`['card_form_plan_start_date']))
What I Wanted - 04/2019
Actual Result - 01/1970
strtotime won't recognise April/2019 as a date. Instead, you can use date_create_from_format with the F/Y format, then reformat the output as m/Y:
echo date_create_from_format('F/Y', 'April/2019')->format('m/Y');
Output
04/2019
Demo on 3v4l.org
You need to replace / with -
date('m/Y', strtotime(str_replace('/','-',$_POST`['card_form_plan_start_date'])))

PHP short date to long date issue [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I'm writing some code a script, which has short dates like
14/12/17
30/11/17
20/11/17
I need to convert these to long date format so i used PHP date function and strtotime as below
echo date('d-m-Y',strtotime('14/12/17'));
But it always getting 01-01-1970 as output, but it should be 14-12-2017
anyone know how to convert this to a long date format please.
PS. Other question answers suggest change the date input format, but I cannot change date input since it's getting from another site
This is from the link I posted and OP says is not correct.
Originally posted by ceiroa.
Convert one date format into another in PHP
$myDateTime = DateTime::createFromFormat('d/m/y','14/12/17');
$newDateString = $myDateTime->format('d-m-Y');
Echo $newDateString;
https://3v4l.org/2AcAd
Use preg_replace to flip the day and month on the incoming dates, then use date as you want.
$orig = ['14/12/17', '30/11/17', '20/11/17'];
foreach ($orig as $s) {
echo date('d-m-Y', strtotime(preg_replace('#(\d+)\/(\d+)\/(\d+)#', '$2/$1/$3', $s)))."\n";
}

PHP Convert Date/Time Format to another [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a Date/time value as such;
2014-01-07T16:19:08Z
I wish to convert it to the format below using PHP
2015-03-21 02:12:01
I know I could use string replace twice over to remove the T and Z characters but doesn't seem right..
use strtotime() with date()
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
The date_format() function returns a date formatted according to the specified format.
$date=date_create("2014-01-07T16:19:08Z");
echo date_format($date,"Y-m-d H:i:s");
One more way is there
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
Update
The date_create() function returns a new DateTime object.
Reference
<?php
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
?>

Change date 2013-08-09 => 09-Aug-2013 [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I want to change format of date by 2013-08-09 to 09-Aug-2013, I tried this:
echo $date=date("Y-m-d");
//output 2013-08-09
echo date("d-M-Y",mktime(0-0-0,$date));
but this code showing 10-Aug-2013
I dont know why this showing date 10 instead of 09.
Answer would be highly appreciated , thanks in advance
following is easier:
echo date("d-M-Y",strtotime($date));
at mktime you have to subtract one day
You aren't passing in the parameters to mktime properly. mktime requires 6 integer parameters where as you have 1 integer and 1 string here.
Try using a DateTime object instead:
$date = new DateTime();
echo $date->format('d-M-Y');

Time Format : PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Covert time format in php
I am getting a start_time field value from mysql data base as 2012-08-14 21:30:00
and i want to convert it in php format like 2012-08-14T09:30 is there any method to do
this in php ?
use strtotime()
$date = strtotime('2012-08-14 21:30:00');
echo date('Y-m-d\Th:i',$date);
see this example.for the required date & time format:
$date=date("Y-m-d H:i:s");//get the current date
$d=strtotime($date);//convert in strtotime
echo $final_date=date("Y-m-d\Th:i:s",$d);//in the first argument of date ,put the format whatever you want,but be sure to convert it in strtotime first.

Categories