Jump to content

date conversion


jas4

Recommended Posts

Hi, i've got a date coming into my webstite in the format:

 

HH:MM:SS DD Mmm YY,YYYY PST (looks like 4:29:05 Aug 23, 2007 PDT)

 

but I want it to convert it into a format for inserting into mysql either in DATE or TIMEDATE,

 

i thought about using explode, but there explode takes everything seperated by a certain character, but how can it be done if there are 2 different characters?

Link to comment
Share on other sites

function  date2date($date)
{
$date = explode(" ", $date);  #hh:mm:ss # dd # mmm# yy,yyyy # PDT
return $date[3] . "-" . $date[2] . "-" . $date[1] . ", " . $date[0];
}

 

to use.

$string = "HH:MM:SS DD Mmm YY,YYYY PST";

date2date($string);

Link to comment
Share on other sites

ok that works pretty well, but for it to be perfect (for my needs ::)) I need to convert the month, i.e. Aug to a number i.e. 08.

 

This is what is returned using the code above (thanks!)

 

String 14:29:05 Aug 23, 2007 PDT

new:  2007-23,-Aug, 4:29:05

Link to comment
Share on other sites

Try something like this:

<?php
function convertMyDate($date) {
  if (preg_match('|^([^\s]+)(.*)$|', $match)) {
    $time = $match[0];
    $date = $match[1];

    if (($ts = strtotime($date . $time)) !== FALSE) {
      return date('Y-d-m g:i:s', $ts);
    } else return FALSE; // invalid format
  } else return FALSE; // invalid format
}
?>

 

Hope that helps. Get familiar with the date/time conversion functions, and you can do almost anything you want. While this may not be exactly what you're after, it should definitely give you a push in the right direction.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.