Page 1 of 1
I need some help bad, please help!
Posted: Fri Sep 06, 2002 10:25 pm
by teamparadox
My website needs this new script to go live, but the following code
$date = $row["date"];
$ndate = explode("/",$date);
$ndate = mktime(0,0,0,$ndate[0],$ndate[1],$ndate[2]);
$date = strftime("%B %d, %Y",$ndate);
print $date;
Is saying the date is October 6th 2003, but the info in the database says sept 6th 2002, something is wrong with my code, im new at this and i cant figure it out, can anyone help?
Posted: Fri Sep 06, 2002 11:06 pm
by volka
maybe some timezone settings.
How about requesting the appropriate date format from the database?
If you're using mysql
$query = "SELECT DATE_FORMAT(myDatefield, '%M %e %y') FROM myTable";
nope
Posted: Sat Sep 07, 2002 12:14 am
by teamparadox2k
the date string the code is handling is from MySQL, its set when the user posts, i checked that and its all correct, its just somewhere in that codeitself thats wrong
Posted: Sat Sep 07, 2002 12:29 am
by sjunghare
Following check should before proceed :
1. Order of parameter in mktime(hr,min,sec,mnt,day,yr)
2. Array [$ndate] index and values
3. When inserting the some time input we provide is incorrect
When I work on the code with my database which stored the date in
Column : name - expirydate
type - datetime
Code: Select all
$date = $rowsї0];
$ndate = explode("/",$date);
echo $ndateї0]."<br>"; //output => Complete Date
echo $ndateї1]."<br>"; //output => Nothing
echo $ndateї2]."<br>"; // output => Nothing
$ndate = mktime(0,0,0,$ndateї0],$ndateї1],$ndateї2]);
echo "-".$ndate."<br>"; // output => -1
$date = strftime("%B %d, %Y",$ndate);
echo $date;//output => Nothing
I gets strange result with
1. No value in the array for index 1,2
2. mktime output give -1
regards,
Sachin
Posted: Sat Sep 07, 2002 2:07 am
by volka
depends on the database's datetime format.
Using mysql you would receive a string like '2002-09-07 09:03:28' that explode("/",...) cannot separate.
humph
Posted: Mon Sep 09, 2002 12:46 pm
by teamparadox2k
ok if the format the batabase is saving in is 22:26 09/06/02 then what is wrong with the code?
Posted: Mon Sep 09, 2002 12:50 pm
by kcomer
If you explode the string 22:36 11/23/2002 or whatever you would have to strip the time part first and also trim it. Why not just use your dbms to give you the correct date. It saves coding and even runs faster.
Keith
.
Posted: Mon Sep 09, 2002 11:05 pm
by teamparadox2k
because i do not yet know php, my programmer has fallen off the planet and this needs to go live and i couldnt figure out the problem myself, so i turned to a php forum for help
Posted: Tue Sep 10, 2002 12:58 am
by Takuma
What will it say if you echo "$row['date']"?
Posted: Tue Sep 10, 2002 1:02 am
by volka
ok if the format the batabase is saving in is 22:26 09/06/02 then what is wrong with the code?
you have a 2-digit-only year format for DATETIME??? That's a problem I would say.
nevertheless I think this should do
Code: Select all
preg_match('!(\d{2})/(\d{2})/(\d{2})!', $rowї0], $ndate);
$ndate = mktime(0,0,0,$ndateї1],$ndateї2],$ndateї3]);
$date = strftime("%B %d, %Y",$ndate);
echo $date;
but still: requesting the proper format from the DB is better

Posted: Tue Sep 10, 2002 1:05 am
by Takuma
Can't you do just this?
Code: Select all
<?php
$ndateї3] = "20"+$ndate;
$ndate = mktime(0,0,0,$ndateї1],$ndateї2],$ndateї3]);
$date = strftime("%B %d, %Y",$ndate);
echo $date;
?>
Posted: Tue Sep 10, 2002 1:41 am
by volka
the mktime of (at least my version of) php can live with the 2-digit-year.
I even produces consistent output on '99'. So I won't mess with the string.
But maybe alter the pattern to '!(\d{2})/(\d{2})/(\d{2,4})!'
Posted: Tue Sep 10, 2002 5:10 am
by mikeq
Have you tried using the StrToTime()
function
Takes a date string and turns it into a UNIX timestamp.