Solved

PHP date conversion needed

Posted on 2012-12-28
9
263 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
$d= "Friday, 28 December, 2012";
echo date('Y-m-d',strtotime($d));
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
@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
Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 15

Expert Comment

by:Insoftservice
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
@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 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
It worked!  Very awesome!  

Thank you!!  

JC
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
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 dynamically set the form action using jQuery.

762 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now