Solved

PHP date conversion needed

Posted on 2012-12-28
9
271 Views
Last Modified: 2013-01-02
PHP Date comparision.

I have a date string that uses this format:

Examples:
Friday, 28 December, 2012
Sunday, 25 December 2012
Monday, 1 January 2013

I need to the above date strings converted to a the following date format: "Y-m-d", so I compare it to that date format (today''s date).

Since the day is in front, when I convert it - it gives me the wrong date. So my function code is off.  



Thanks!

JC
0
Comment
Question by:567082836
[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
  • 5
  • 2
  • 2
9 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38728664
PHP strtotime() and date() are your friend.  Please read this and then post back with any questions.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38729061
Please see http://www.laprbass.com/RAY_temp_567.php
<?php // RAY_temp_567.php
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT EE
$dates = <<<END
Friday, 28 December, 2012
Sunday, 25 December 2012
Monday, 1 January 2013
END;

// REQUIRED FOR TZ/DATE/TIME FUNCTIONS
date_default_timezone_set('America/Chicago');

// PROCESS EACH OF THE DATES
$arr = explode(PHP_EOL, $dates);
foreach ($arr as $date)
{
    // REMOVE THE DAY OF THE WEEK
    $xxx  = explode(',', $date);
    $date = trim($xxx[1]);

    // CONVERT TO UNIX TIMESTAMP
    $timestamp = strtotime($date);

    // REFORMAT THE TIMESTAMP ACCORDING TO A PATTERN
    $iso8601   = date('c', $timestamp);
    $ymdstring = date('Y-m-d', $timestamp);

    // SHOW THE WORK PRODUCT
    echo PHP_EOL;
    echo $date;
    echo ' = ';
    echo $ymdstring;
    echo ' = ';
    echo $iso8601;
}

Open in new window

HTH, ~Ray
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 38729132
$d= "Friday, 28 December, 2012";
echo date('Y-m-d',strtotime($d));
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38729159
@insoftservice: The issue here is that the first information to the strtotime() function controls the output from the function.  To see it in action, try these dates.  Then you can see why I recommend removing the day of the week.  It doesn't work if the day of the week is present!  That is the central issue that the author noted.

Friday, 27 December, 2012
Friday, 28 December, 2012
Friday, 29 December, 2012
0
 
LVL 15

Expert Comment

by:Insoftservice
ID: 38729394
ya @Ray you are right thx for info.

$d= "Sunday, 25 December 2012";
$dt = explode(',',$d);
echo date('Y-m-d',strtotime($dt[1]));
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 38729401
It's kind of goofy behavior from PHP strtotime() -- It seems to me that a date that cannot exist should return FALSE.  I added a manual note to the strtotime() page on PHP.net.  It should show up in a few days.
0
 

Author Comment

by:567082836
ID: 38737619
@Ray
I just tried your code.. so close.. "explode function" it is stripping out the Day of the Week but also the year.  So for example: "Friday, 27 December, 2012" turns into  2013-12-15.  It should be 2012-12-15.  The year is off.

@insoftservice,
I just tried your code but my date string is actually this:
Friday, 27 December, 2012
You can see there are 2 commas in this string.  So when I tested your code it removed FRIDAY and the year 2012.  Which outputted the wrong year.

You guys are very close.. if possible see if you can make some changes..

Thank you!

JC
0
 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 38738481
Maybe you should have tried it last year ;-)  Here is a better approach.
http://www.laprbass.com/RAY_temp_567.php
<?php // RAY_temp_567.php
error_reporting(E_ALL);
echo '<pre>';

// TEST DATA FROM THE POST AT EE
$dates = <<<END
Friday, 28 December, 2012
Sunday, 25 December 2012
Monday, 1 January 2013
END;

// REQUIRED FOR TZ/DATE/TIME FUNCTIONS
date_default_timezone_set('America/Chicago');

// PROCESS EACH OF THE DATES
$arr = explode(PHP_EOL, $dates);
foreach ($arr as $date)
{
    // REMOVE THE DAY OF THE WEEK
    $xxx  = explode(',', $date);
    unset($xxx[0]);
    $date = trim(implode(' ', $xxx));

    // CONVERT TO UNIX TIMESTAMP
    $timestamp = strtotime($date);

    // REFORMAT THE TIMESTAMP ACCORDING TO A PATTERN
    $iso8601   = date('c', $timestamp);
    $ymdstring = date('Y-m-d', $timestamp);

    // SHOW THE WORK PRODUCT
    echo PHP_EOL;
    echo $date;
    echo ' = ';
    echo $ymdstring;
    echo ' = ';
    echo $iso8601;
}

Open in new window

Best, ~Ray
0
 

Author Comment

by:567082836
ID: 38738553
It worked!  Very awesome!  

Thank you!!  

JC
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

756 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