PDA

View Full Version : Converting this timestamp to a more readable date


woofy
12-09-2010, 06:15 PM
I have the following php code to display the date when a entry was submitted into the database
<?php echo $row_rsNEW['date']); ?>

It dispalys something that looks like this - 2010-12-07 16:47:51

How can I command it to display something that looks like- 12-7-10 4:47 PM ?

I have tried several different methods but I must be writing it wrong.

edbr
12-10-2010, 12:13 AM
Time:
a: am or pm depending on the time
A: AM or PM depending on the time
g: Hour without leading zeroes. Values are 1 through 12.
G: Hour in 24-hour format without leading zeroes. Values are 0 through 23.
h: Hour with leading zeroes. Values 01 through 12.
H: Hour in 24-hour format with leading zeroes. Values 00 through 23.
i: Minute with leading zeroes. Values 00 through 59.
s: Seconds with leading zeroes. Values 00 through 59. Day:
d: Day of the month with leading zeroes. Values are 01 through 31.
j: Day of the month without leading zeroes. Values 1 through 31
D: Day of the week abbreviations. Sun through Sat
l: Day of the week. Values Sunday through Saturday
w: Day of the week without leading zeroes. Values 0 through 6.
z: Day of the year without leading zeroes. Values 0 through 365. Month:
m: Month number with leading zeroes. Values 01 through 12
n: Month number without leading zeroes. Values 1 through 12
M: Abbreviation for the month. Values Jan through Dec
F: Normal month representation. Values January through December.
t: The number of days in the month. Values 28 through 31. Year:
L: 1 if it's a leap year and 0 if it isn't.
Y: A four digit year format
y: A two digit year format. Values 00 through 99.

woofy
12-10-2010, 03:06 AM
What part of the echo string do they go in, I am getting all kinds of errors.

I thought it would be

<?php echo (("Y-m-d" $row_rsNEW['date'])); ?>

Do I have something wrong with the syntax?

edbr
12-10-2010, 04:31 AM
yes, sory but i'm im very pressed for time , have a look at tizag .com for examples

jmichae3
12-10-2010, 05:40 AM
//various DB field conversion functions
function ampm($tm) {
//convert MySQL 24-hour time format 00:00:00 to 12-hour format
if ($tm=="") {
return "12:00:01am";
}
$ab = explode(":", $tm);

$ap="";
switch(count($ab)) {
case 1:
$ab[0]=intval($ab[0]);
$ap = $ab[0]<12?" am":" pm";
if (0 == $ab[0]) {$ab[0]=24;}
if ($ab[0]>12) {$ab[0]-=12;}
return sprintf("%02d:%02d:%02d%s", $ab[0], 0, 0, $ap);
break;
case 2:
$ab[0]=intval($ab[0]);
$ab[1]=intval($ab[1]);
$ap = $ab[0]<12?" am":" pm";
if (0 == $ab[0]) {$ab[0]=24;}
if ($ab[0]>12) {$ab[0]-=12;}
return sprintf("%02d:%02d:%02d%s", $ab[0], $ab[1], 0, $ap);
break;
case 3:
$ab[0]=intval($ab[0]);
$ab[1]=intval($ab[1]);
$ab[2]=intval($ab[2]);
$ap = $ab[0]<12?" am":" pm";
if (0 == $ab[0]) {$ab[0]=24;}
if ($ab[0]>12) {$ab[0]-=12;}
return sprintf("%02d:%02d:%02d%s", $ab[0], $ab[1], $ab[2], $ap);
break;
default:
return "12:00:02am";
break;
}
}

function militarytime($t) {
//convert 12-hour time format to MySQL 24-hour time format
$i=strpos($t,"a");
if (false==$i){$i=strpos($t,"A");}
if (false==$i){$i=strpos($t,"p");}
if (false==$i){$i=strpos($t,"P");}
//echo $i;
$tm=array(); //space-separated strings
$tms=array('00','00'); //time components separated by :
if ($i) { //has am/pm
if (strstr($t," ")) {
$tm=explode(" ",$t);
$tms=explode(":",$tm[0]);
} else {//no space before am/pm. example: 1:00a
$tms=explode(":",substr($t,0,$i)); //1:00
$tm[0]=implode(":",$tms);
}
//echo $t[$i];
if ('a'==strtolower($t[$i])) {
$t=$tm[0];
//correct the hour
if ('12'==$tms[0]) { $tms[0]='00'; }
} else { //pm
//correct the hour
if ('12'!=$tms[0]) { $tms[0]+=12; }
}
if (count($tms)<2) {
$tms[1]='00';
}
return implode(":",$tms);
}
//already in military time
return $t;
}

function date2mysql($d) {
//convert USA style mm/dd/yy date to MySQL DB-friendly yyyy-mm-dd
if (strstr($d,"/")) {
$dt=explode("/",$d);
switch(count($dt)) {
case 3: //specified all 3.
if (strlen($dt[2])==2) {$dt[2]="20$dt[2]";} //make it a 4-digit year 2000+ thing
$d="$dt[2]-$dt[0]-$dt[1]"; break;
case 2: //they left the year out
$today = getdate();
$year=$today['year'];
$d="$year-$dt[0]-$dt[1]";
break;
case 1: //year only
if (strlen($dt[0])==2) {$dt[0]="20$dt[0]";} //make it a 4-digit year 2000+ thing
$d="$dt[0]-01-01"; break;
default: $d="01-01-01";/*$d="2001-01-01";*/ break;
}
}
return $d;
}

function mysql2date($d) {
//convert MySQL date format yyyy-mm-dd to USA style mm/dd/yyyy
if (strstr($d,"-")) {
$dt=explode("-",$d);
$dt[0]-=2000; //make year 10 instead of 2010
if (3==count($dt)) {
return "$dt[1]/$dt[2]/$dt[0]";
} else { //assume 2 - they left the year out
return implode("/",$dt);
}
}
return $dt;
// return date("n/j/Y",strtotime($d));
}
function datetime2mysql($dt) {
//convert USA style mm/dd/yy date to MySQL DB-friendly yyyy-mm-dd
$a=explode(" ",$dt);
$t="";
$d="";
$d0="";
if (3==count($a) && (
"P"==$a[2]||
"PM"==$a[2]||
"pm"==$a[2]||
"Pm"==$a[2]||
"A"==$a[2]||
"AM"==$a[2]||
"Am"==$a[2]||
"am"==$a[2]||
"P.M."==$a[2]||
"p.m."==$a[2]||
"P.m."==$a[2]||
"A.M."==$a[2]||
"A.m."==$a[2]||
"a.m."==$a[2]
)) {
$d=$a[0];
$t=militarytime($a[1] . $a[2]); //assume 3rd parameter is am or pm
} else if (2==count($a)) {
$d=$a[0];
$t=militarytime($a[1]);
} else {
return "0000-00-00 00:00:00";
}
if (strstr($d,"/")) {
$d0=explode("/",$d);
switch(count($d0)) {
case 3: //specified all 3.
$d0[2]=intval($d0[2]);
if ($d0[2]<=38) {
$d0[2]=2000+$d0[2]; //make it a 4-digit year 2000+ thing
} else if ($d0[2] <=99) {
$d0[2]=1900+$d0[2]; //make it a 4-digit year 1900+ thing
} else {
//$d0[2]=$d0[2]; //make it a 4-digit year 1900+ thing
}
$d0=sprintf("%04d-%02d-%02d", intval($d0[2]), intval($d0[0]), intval($d0[1]));
break;
case 2: //they left the year out
$today = getdate();
$year=$today['year'];
$d0=sprintf("%04d-%02d-%02d", intval($year), intval($d0[0]), intval($d0[1]));
break;
case 1: //year only
$d0[0]=intval($d0[0]);
if ($d0[0]<=38) {
$d0[0]=2000+$d0[2]; //make it a 4-digit year 2000+ thing
} else if ($d0[0] <=99) {
$d0[0]=1900+$d0[2]; //make it a 4-digit year 1900+ thing
} else {
//$d0[2]=$d0[2]; //make it a 4-digit year 1900+ thing
}
//$d="$dt[0]-01-01"; break;
$d0=sprintf("%04d-%02d-%02d", intval($d0[0]), 1, 1);
default:
$d0="0000-00-00";
break;
}
}
return $d0 . " " . $t;
}
function mysql2datetime($dt) {
//echo "\n<div>$dt</div>\n";
$a=explode(" ",$dt);
if (count($a)!=2) {
return "00/00/00 12:00:00am";//return "00/00/0000 12:00:00am";
}
$d=$a[0]; //mysql date 0000-00-00
$t=$a[1]; //24-hour time 00:00:00
if (strstr($t,":")) {
$t=ampm($a[1]);
} else { //totally bogus value
return "00/00/00 12:00:00am";//"00/00/0000 12:00:00am";
}
$d0="";
//convert MySQL date format yyyy-mm-dd to USA style mm/dd/yyyy
if (strstr($d,"-")) {
$d0=explode("-",$d);
$d0[0]-=2000;//make year 2010 as 10
switch(count($d0)) {
case 3:
$d0=sprintf("%d/%d/%d", intval($d0[1]), intval($d0[2]), intval($d0[0]));
break;
case 2: //assume 2 - they left the year out or they left the
if (intval($d0[0]) >=2000) { //it's a year
$d0=sprintf("%d/%d/%d", intval($d0[1]), 1, intval($d0[0]));
} else if ((intval($d0[1]) >=2000)) {
$d0=sprintf("%d/%d/%d", intval($d0[1]), 1, intval($d0[0]));
} else {
$d0="00/00/00";//$d0="00/00/0000";
}
$d0=implode("/",$d0);
break;
case 1:
$d0=sprintf("%d/%d/%d", 1, 1, intval($d0[0]));
break;
default: //totally bogus value
return "00/00/00 12:00:00am";//"00/00/0000 12:00:00am";
break;
}
} else { //totally bogus value
return "1/8/10 12:00:00am";//return "1/8/2010 12:00:00am";
}
return $d0 . " " . $t;
// return date("n/j/Y",strtotime($d));
}

jmichae3
12-10-2010, 05:47 AM
I suggest you don't use 2-digit years. we had a whole y2k bruhaha over that...

edbr
12-10-2010, 06:02 AM
<?php echo date("Y-m-d",strtotime($row_rsNEW['date'])); ?> should do it
nice conversion functions btw

woofy
12-11-2010, 01:41 AM
Thanks, that works great!

jmichae3
12-14-2010, 05:18 AM
I think you specified 2-digit years. that is not a 2-digit year. just keeping the specs.