Page 1 of 1

mktime() abd TIMESTAMP

Posted: Sun Jan 18, 2004 12:40 pm
by apek
in my mysql...my data type for date i set to TIMESTAMP...
and then i use date() function to format it the way i like...
but i'm not sure about the mktime() function i'm using rite now...

this is my snippet of code:

Code: Select all

$mulaServis=$_POST["mulaServis"];
$tamatServis=$_POST["tamatServis"];
$start=mktime($mulaServis,$day, $month, $year);
$end=mktime($tamatServis,$day, $month, $year);
and then i use the $mula and the $tamat in my query to get data between the two date...i mean i want to fetch the data between the range $start and $end ..
this is my query:

Code: Select all

$viewall=query("Select * from preventiveMaint WHERE preventiveMaint.waktuTamat BETWEEN '$start' AND '$end'", $link, __LINE__, __FILE__);
my problem is i the query seems fail..
pls help me...

Posted: Sun Jan 18, 2004 3:35 pm
by Weirdan
It's a common pitfall when using MySQL timestamps. They aren't unix timestamps, they are just concatenated year,month,day,hours,minutes and seconds. Try the following:

Code: Select all

$mulaServis=$_POST["mulaServis"]; 
$tamatServis=$_POST["tamatServis"]; 
$start=date("YmdHis",strtotime($mulaServis));
$end=date("YmdHis",strtotime($tamatServis));

......

Posted: Mon Jan 19, 2004 12:12 pm
by apek
thats working....
but..
:( ...
i have a date taken from a datepicker,the format of the date is like:
01/20/2004

my datatype for date in mysql is timestamp...
but why in the database the date will change to 00000000000000???

i heard mktime can port the date to timestamp format...or is it strtotime??

this is my try:

Code: Select all

$today=date("YmdHis",strtotime($_POST["tarikhMasukan"]));
and the $today wll be inserted to database...
if my code above is wrong,how to fix that?
i read about mktime and strtotime in php.net manual,but i dont understand because theres no sample there..only syntax....
pls help me...
pls help me...

Posted: Mon Jan 19, 2004 6:24 pm
by pickle
hmm, not sure about strtotime, but I do know mktime require arguments like this: mktime(hours,minutes,seconds,month,day,year). Each argument must be 2 characters long, except year which must be 4. Hope this helps.

Posted: Mon Jan 19, 2004 11:10 pm
by apek
ok..but i test this
i want to convert date entered to time stamp format...

Code: Select all

$test=create_timestamp($_POST["tarikhMasukan"]);
then in custom function create_timestamp

Code: Select all

function create_timestamp()
{

  $timestamp = date("HismdY");

  return $timestamp;

}
and then the return result will be used in my INSERT INTO query:

Code: Select all

$sqlquery = query("INSERT INTO preventiveMaint(tarikhMasukan,noTag,idPekerja,waktuMula,masaMula,waktuTamat,masaTamat,hariBD,jamBD,minitBD,jenisOperasi,komponen,kuantiti,kos,maklumatKerja,jenis) VALUES('$test', '{$_POST["noTag"]}', '{$_POST["idPekerja"]}', '{$_POST["mulaServis"]}', '{$_POST["masaMula"]}', '{$_POST["tamatServis"]}', '{$_POST["masaTamat"]}', '{$_POST["hariBD"]}', '{$_POST["jamBD"]}', '{$_POST["minitBD"]}', '{$_POST["jenisOperasi"]}', '{$_POST["komponen"]}', '{$_POST["kuantiti"]}', '{$_POST["kos"]}', '{$_POST["kerja"]}','{$jenis}')");
but it is weird why in mysql database still showing
00000000000000???


?>

Posted: Mon Jan 19, 2004 11:19 pm
by timvw
if you want to insert the current time as timestamp into a column, just use the NOW() function in mysql.

Thus "insert into table(atime) values (NOW())";

If you want to select it, use date_format
"select date_format(atime,'%Y-%m-%d %H:%i:%s') from table"
(More info about this function -> mysql.com/manual ;)

This way you avoid all problems that can arise ;)

Posted: Mon Jan 19, 2004 11:21 pm
by apek
but the problem is, i have alot more dates than today or NOW dates...
all of the above was just an example...
so how to do it? :cry:

Posted: Tue Jan 20, 2004 1:00 am
by mathewvp
i have a date taken from a datepicker,the format of the date is like:
01/20/2004
You need to convert it into "Year month date format";

Code: Select all

$newdate=explode("/",$_POSTї"tarikhMasukan"]);    
$today=$newdateї2]."-".$newdateї0]."-".$newdateї1];

$start=date("YmdHis",strtotime($today));

its works!!!!

Posted: Tue Jan 20, 2004 1:35 am
by apek
its works!!!!

Code: Select all

$newdate=explode("/",$_POST["tarikhMasukan"]);   
$today=$newdate[2]."-".$newdate[0]."-".$newdate[1];
but what the [2],[0] and [1] mean?

Posted: Tue Jan 20, 2004 2:56 am
by mathewvp
Your date from datepicker is in the format 01/20/2004 ie month/day/year.
explode splits it and stores month in $newdate[0],day in$newdate[1] and year in $newdate[2].You take that and make it into year-month-day format.ie 2004-01-20.