Wrox Programmer Forums
|
PHP How-To Post your "How do I do this with PHP?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the PHP How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 18th, 2004, 09:11 AM
Authorized User
 
Join Date: Jan 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default UNIX Timestamp

quasedilla5 once said:
 
Quote:
quote:
Quote:
If you want a custom time, I would suggest changing the field type to CHAR (a fixed character length) And use PHP to generate a timestamp, I would suggest using UNIX timestamps which are a bit more compatible with the PHP functions mktime() and date() and just IMO more flexible overall. date() will take a UNIX timestamp and format it however you like.

More information:
http://www.php.net/date
http://www.php.net/mktime
http://www.mysql.com/doc/en/DATETIME.html
my date field is in char datatype...
i dont understand what is UNIX timestamp and what is advantages...
if i want to manipulate my date like counting different between days,use BETWEEN in query,how to do that if my datatype is in CHAR???


 
Old January 18th, 2004, 03:16 PM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

Hi Apek,

This is what a UNIX timestamp looks like:
1074402000

(Today)

Actually if you are going to use a UNIX timestamp I would use an INT field type... I don't know what tangent I was off on when I suggested CHAR field type.

With an INT field type you can make queries like this:

SELECT * FROM event WHERE time < 1074402000 AND time > 1072933200;

Basically it says get all the records between these times Less than Jan 18 and Greater than Jan 1.

MySQL also has a function to insert UNIX timestamps and that is:
UNIX_TIMESTAMP()

http://www.mysql.com/doc/en/Date_and...functions.html

The PHP manual pages that I provided elaborate on how you may format a UNIX timestamp to your liking with the date() function. And how you may create a UNIX timestamp with the mktime() function, and as such I won't elaborate more on that.

: )
Rich

:::::::::::::::::::::::::::::::::
Smiling Souls
http://www.smilingsouls.net
:::::::::::::::::::::::::::::::::
 
Old January 19th, 2004, 02:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A shorter answer: A UNIX timestamp is an integer that holds the number of seconds that have elapsed since the epoch*. The UNIX epoch is midnight (00:00:00am) January 1, 1970 GMT.

* http://foldoc.doc.ic.ac.uk/foldoc/fo...gi?query=epoch


Take care,

Nik
http://www.bigaction.org/
 
Old January 20th, 2004, 01:50 AM
Authorized User
 
Join Date: Jan 2004
Posts: 33
Thanks: 0
Thanked 0 Times in 0 Posts
Default

i want to convert date entered to time stamp format...

Code:
$test=create_timestamp($_POST["tarikhMasukan"]);



then in custom function create_timestamp
Code:
function create_timestamp()
{

  $timestamp = date("HismdY");

  return $timestamp;

}



and then the return result will be used in my INSERT INTO query:
Code:
$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???

 
Old January 20th, 2004, 02:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

What exactly is the data type of your column? You said it's a char type, but you didn't specify the length.

You're confusing a few things with your code. You pass a parameter to create_timestamp() but you don't use it. That means that that you're always creating a date/time string for the exact time that the function is called. Also, you're not creating a timestamp in that function, you're creating a date string. The format of this string is also pretty strange -- you don't have any separators in the string to distinguish all the different parts.

For example, if I were to run your function RIGHT NOW (Jan 20, 2004 10:04:00 am), your create_timestamp() function would return this string:
  "10040001202004"

There are a couple things you need to notice about this value. First, it's 14 characters long. 2nd, if it's converted to an integer, it becomes much larger than the largest 4-byte integer can store.


If you're dealing with dates and/or times in MySQL, you should use either an unsigned integer (to hold a unix timestamp), or a DATE, TIMESTAMP, or DATETIME field type, depending on what you're storing.

Then, you'd want to rewrite your create_timestamp() function to translate the incoming parameter to the format the database expects.


Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
TimeStamp nay_ctg BOOK: Beginning Cryptography with Java 1 August 7th, 2008 07:37 PM
GridView + timestamp victorsj ASP.NET Espanol 0 August 29th, 2006 10:43 AM
GridView + timestamp victorsj ASP.NET 2.0 Professional 0 August 29th, 2006 10:41 AM
XSLT unix timestamp conversion Weezel XSLT 2 January 28th, 2005 02:12 PM
timestamp isheikh BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 1 November 18th, 2004 06:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.