Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

PHP date conversion needed

Posted on 2012-12-28
9
Medium Priority
?
275 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
  • 5
  • 2
  • 2
9 Comments
 
LVL 111

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 111

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 111

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 111

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 111

Accepted Solution

by:
Ray Paseur earned 2000 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 count occurrences of each item in an array.
Suggested Courses

877 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