Welcome Guest, Not a member yet? Register   Sign In
Need Help With Formatting Date
#1

[eluser]nagolpj[/eluser]
My web site is completly coded with CI and I need help formatting the output of dates stored in MySQL. Currently, the date is stored in the DB as YYYY-MM-DD but in order for the exported file to correctly import into my software, I need to date to be formatted MM/DD/YYYY. I think the data posts in the correct format but then changes since the column is a DATE column. I don't know exactly though.

Anyway, I assume the fix lies within this piece of code:

Code:
foreach ($result as $row)
        {
            if ($row['Event']['type'] != 'Relay')
            {
                $output .= "E;";
                $output .= $row['Athlete']['lname'].";";
                $output .= $row['Athlete']['fname'].";";
                $output .= ";";
                $output .= $row['Athlete']['gender'].";";
                $output .= $row['Athlete']['birthday'].";";
                $output .= $row['School']['abbr'].";";
                $output .= $row['School']['name'].";";
                $output .= ";";
                $output .= $row['Athlete']['grade'].";";
                $output .= $row['Event']['code'].";";
                $output .= $row['Meet_Athlete']['mark'].";";
                $output .= (($row['Event']['type'] == 'Field') ? "E": "M").
                    ";;\r\n";
            }
        }

Specifically, this line:

Code:
$output .= $row['Athlete']['birthday'].";";

And in case you are wondering, the data get's exported to a semicolon delimited file so that's why you see so many semicolons.

Any idea how I would alter this code to get the format that I need?
#2

[eluser]nagolpj[/eluser]
So I am now loading the date helper and I'm using this piece of code:

Code:
$datestring = "%m/%d/%Y";
                    $date = $row['Athlete']['birthday'];
                $output .= mdate($datestring, $date).";";

The format is right but the date is displayed as 12/31/1969. What's up with that?
#3

[eluser]dawnerd[/eluser]
Another way you can do it is like so:
Code:
$date = date("m/d/Y",strtotime($row['Athlete']['birthday']));
#4

[eluser]nagolpj[/eluser]
Thank you thank you thank. I've been beating my head over here. I really appreciate this.
#5

[eluser]nagolpj[/eluser]
I had to remove "strtotime" from the code to get it to work. It suddenly started giving me errors. I don't know why.
#6

[eluser]nagolpj[/eluser]
So I guess I needed the "strtotime" function. And I think I'm getting errors becaus enot all the athletes have birthdays entered.

Code:
A PHP Error was encountered
Severity: Notice

Message: strtotime() [function.strtotime]: Called with empty time parameter

Filename: admin/reports.php

Line Number: 119

What can I do about this?
#7

[eluser]jamesf[/eluser]
I create a small helper function that takes a MySQL date and outputs a date in the format i want...

Code:
function print_date($date) {
    // parse the date and time
    $year = substr($date, 0, 4);
    $year1 = substr($date, 2, 2);
    $month = substr($date, 5, 2);
    $day = substr($date, 8, 2);
    $hour = substr($date, 11,2);
    $min = substr($date, 14, 2);
return "{$hour}:{$min} - {$day}/{$month}/{$year}";
}

If your date was empty you could code up a small check e.g.

Code:
if(!empty($date)) {
return formatted_date;
} else {
return '';
}
#8

[eluser]louis w[/eluser]
strtotime is a built in php function. if you are getting errors, my first guess is you were not passing it a valid string.
#9

[eluser]megabyte[/eluser]
Why not just use MySql and let it do all the work? I myself once upon a time used PHP for everything, but MySql makes it so much easier.


Code:
SELECT DATE_FORMAT(your_date_field, '%m/%d/%Y') as formated_date




Theme © iAndrew 2016 - Forum software by © MyBB