Solved

PHP date conversion needed

Posted on 2012-12-28
9
272 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

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 i…
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

751 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