Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PHP date conversion needed

Posted on 2012-12-28
9
Medium Priority
?
274 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 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
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.

705 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