Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String to DateTime Object

I'm new at String functions, so I need a complex substr and trim functions for this string:

Wed, 28 Dec 2011 13:04:30 GMT

String comes to me always with this format. I want to convert it to DateTime object. Anybody can help me?

like image 694
MAB Avatar asked Dec 28 '11 13:12

MAB


People also ask

How do you convert a string to a datetime object in Python?

We can convert a string to datetime using strptime() function. This function is available in datetime and time modules to parse a string to datetime and time objects respectively.

How do I convert a string to a date?

Using strptime() , date and time in string format can be converted to datetime type. The first parameter is the string and the second is the date time format specifier. One advantage of converting to date format is one can select the month or date or time individually.

How do I convert a string to a timestamp in Python?

Import the datetime library. Use the datetime. datetime class to handle date and time combinations. Use the strptime method to convert a string datetime to a object datetime.

What is Strftime and Strptime in Python?

Python time strptime() MethodThe format parameter uses the same directives as those used by strftime(); it defaults to "%a %b %d %H:%M:%S %Y" which matches the formatting returned by ctime(). If string cannot be parsed according to format, or if it has excess data after parsing, ValueError is raised.


Video Answer


2 Answers

$dateString = 'Wed, 28 Dec 2011 13:04:30 GMT';
$dateTime = datetime::createfromformat('D, d M Y H:i:s e',$dateString);

echo $dateTime->format('d-M-Y H:i:s e');
like image 185
Mark Baker Avatar answered Oct 24 '22 18:10

Mark Baker


<?php
$date = new DateTime('Wed, 28 Dec 2011 13:04:30 GMT');
echo $date->format('r');

... prints:

Wed, 28 Dec 2011 13:04:30 +0000
like image 34
Álvaro González Avatar answered Oct 24 '22 17:10

Álvaro González