Solved

How to convert PHP String into DateTime to use in mySQL

Posted on 2013-02-06
5
437 Views
Last Modified: 2013-02-06
I have a string that I need to convert into a datetime. The string shows the date and time like below.

2/6/2013 11:47

I need to convert this into Y-m-d H:i:s

I have tried a couple of things with no luck so far. Wondering if anyone had a quick solution. Thanks
0
Comment
Question by:ats2012
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 38861058
<?php
error_reporting(E_ALL);

$s = '2/6/2013 11:47';
$date = strtotime($s);
echo date('d/M/Y H:i:s', $date);
?>
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38861059
See here: http://www.php.net/manual/en/function.strtotime.php and here: http://www.php.net/manual/en/function.date.php    Please read the notes for 'strtotime' because there are some limits in what it will understand.  

$nudate = strtotime("2/6/2013 11:47");
echo date("Y-m-d H:i:s", $nudate);

Open in new window

0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 38861062
Sorry,
<?php
error_reporting(E_ALL);

$s = '2/6/2013 11:47';
$date = strtotime($s);
echo date('Y-m-d H:i:s', $date);
?>

Open in new window

0
 

Author Closing Comment

by:ats2012
ID: 38861077
Works Perfect. Thank you very much.
0
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 38861163
Really?  That is essentially identical to my answer.
0

Featured Post

Why You Need a DevOps Toolchain

IT needs to deliver services with more agility and velocity. IT must roll out application features and innovations faster to keep up with customer demands, which is where a DevOps toolchain steps in. View the infographic to see why you need a DevOps toolchain.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question