473,696 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

converting Date() to unix timestamp format?

hi

im trying to convert Date() into a unix timestamp so i can stick the
result into a mysql db, please help!

Mar 6 '07 #1
6 34089
Lee
ma**@gaspdesign .co.uk said:
>
hi

im trying to convert Date() into a unix timestamp so i can stick the
result into a mysql db, please help!
I've been using Unix since before you were born (I can tell by
the fact that you haven't learned to use the SHIFT key), and
have no idea what you mean by a "unix timestamp" and don't have
any inclination to look at mysql documentation to see what they
think it is.

Give an example of what you want.
--

Mar 6 '07 #2
Lee wrote:
m...@gaspdesign .co.uk said:
>im trying to convert Date() into a unix timestamp so i can
stick the result into a mysql db, please help!

I've been using Unix since before you were born (I can tell by
the fact that you haven't learned to use the SHIFT key), and
have no idea what you mean by a "unix timestamp"
Then you should definitely start learning about it!

http://en.wikipedia.org/wiki/Unix_Timestamp
http://www.unixtimestamp.com
and don't have any inclination to look at mysql documentation
to see what they think it is.
The Unix timestamp is well-known and defined; it is the number of
seconds since Epoch. But the MySQL timestamp is different, that uses
the YYYYMMDDHHmmSS-format. Since the objective is to store a timestamp
in MySQL, one should opt to play by the MySQL rules here. Suppose this
table:

CREATE TABLE `tt` (`a` timestamp(14) NOT NULL);

If the OP's intention is to insert the current time in db -which I
suspect- then just do:

INSERT INTO `tt` VALUES ( NOW() );

And then one could read it out as Unix timestamp:

SELECT UNIX_TIMESTAMP( a) FROM tt;

No need for any javascript or shell commands.

If the intention is to store a timestamp that is not the current one,
then let javascript's Date() calculate the specific time of your
interest in (MySQL's) YYYYMMDDHHmmSS-format, like eg. 20040318140523
for March 18 2004, 14:05:23. Then do:

INSERT INTO `tt` VALUES ( `20040318140523 ` );

Alternatively, in stead of using Date() in javascript, one could use
MySQL's date functions, which might be experienced as more terse and
powerful:

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

--
Bart

Mar 6 '07 #3
Lee
Bart Van der Donck said:
>
Lee wrote:
>m...@gaspdesig n.co.uk said:
>>im trying to convert Date() into a unix timestamp so i can
stick the result into a mysql db, please help!

I've been using Unix since before you were born (I can tell by
the fact that you haven't learned to use the SHIFT key), and
have no idea what you mean by a "unix timestamp"

Then you should definitely start learning about it!

http://en.wikipedia.org/wiki/Unix_Timestamp
http://www.unixtimestamp.com
I've never heard Epoch time called "Unix timestamp". Maybe the
term is popular with non-Unix users?

It's also obviously, from your further description, not what
the OP meant.
--

Mar 6 '07 #4
Lee wrote:
I've never heard Epoch time called "Unix timestamp". Maybe the
term is popular with non-Unix users?
With Unix users in the first place, I'ld say...
It's also obviously, from your further description, not what
the OP meant.
The original poster wants to insert a timestamp in MySQL. The
recommended way to do that, is to use MySQL's TIMESTAMP data type,
which is intended especially for that purpose. MySQL knows the
UNIX_TIMESTAMP( ) function for quick&easy over-and-back conversions
between Unix' and MySQL's timestamps.

--
Bart

Mar 6 '07 #5
Lee
Bart Van der Donck said:
>
Lee wrote:
>I've never heard Epoch time called "Unix timestamp". Maybe the
term is popular with non-Unix users?

With Unix users in the first place, I'ld say...
Unix users don't call "ls" "the Unix ls command". They call it "ls".
Similarly, Unix users (the many that I know, at least) aren't likely
to call epoch time "the Unix timestamp".

If there's a mysql function called UNIX_TIMESTAMP( ), then there's
a pretty good clue as to where the term originated.
--

Mar 6 '07 #6
In comp.lang.javas cript message <11************ **********@p10g 2000cwp.go
oglegroups.com> , Tue, 6 Mar 2007 08:24:03, "ma**@gaspdesig n.co.uk"
<ma**@gaspdesig n.co.ukposted:
>im trying to convert Date() into a unix timestamp so i can stick the
result into a mysql db, please help!
In Javascript, Date() gives a string, and of undefined format (ISO 16262
15.9.2 &c). It's not a good starting-point for most requirements.

But new Date() gives a Date Object, and so + new Date() gives
the number of milliseconds (ignoring Leap Seconds, but not Summer Time)
since the Javascript Epoch. Converting that to the number of seconds
since the UNIX Epoch can be left as an exercise.

If you want a MySQL format from a Javascript group, you should give its
definition. Does it require to be in UTC or in local, with/without
offset in what form? In any case, reading the newsgroup FAQ should put
you in the right direction.

If you require a string YYYYMMDDhhmmss, it's easiest to use, in a string
context, Y*1e10 + M*1e8 + D*1e6 + h*1e4 + m*1e2 + s*1e0 .
BTW, in the FAQ, it may become desirable to convert from ISBN-10 to the
current ISBN-13 : see <URL:http://www.merlyn.demo n.co.uk/js-misc0.htm>.

It's a good idea to read the newsgroup and its FAQ. See below.

--
(c) John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v6.05 IE 6
news:comp.lang. javascript FAQ <URL:http://www.jibbering.c om/faq/index.html>.
<URL:http://www.merlyn.demo n.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demo n.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Mar 6 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
13147
by: Kit | last post by:
Howdy! I have some dates in a MySQL database that are in a 'Y-m-d' format. Is there a way to have PHP read these dates and convert them to a 'd-M-Y' format?
2
32422
by: Ben | last post by:
I would like to use php to query a database and retrieve a unix timestamp. The problem is that mysql is storing the data in the date format and not a timestamp. I am sure that I can amend my query to format the date returned as a timestamp without having to do the conversion in php. Can someone tell me what to put in my db query?
13
9292
by: perplexed | last post by:
How do you convert a user inputted date to a unix timestamp before insterting it into your database? I have a form, with a textfield for a date that the user inputs in the format mm-dd-yyyy and three dropdow boxes for hours, minutes, and AM/PM. All of these need to be considered together and converted to one Unix Timestamp and then inserted to the MYSQL date field. The type of field is INT (11) so that I can instead of the standard...
11
4072
by: Rick | last post by:
I am going crazy on how to format a simple date. I have this in my MySQL table. "2005-03-10 08:44:21" and I want it to format out like "3/10/2005" or "03/10/05" but I seem to be going at it the wrong way. Here what I have but wow I get a wrong date. $cdate = date("r",$cdate); Please help!! I appreciate any help you can give. Rick
6
39790
by: bryan.seaton | last post by:
I have a delete statement that is not doing what I want it to do: Delete from LOG_TABLE where (DATE(LOG_TS)) < (DATE(CURRENT_DATE)- 21 DAYS); It is supposed to delete all records that are 21 days or older than the current system date. Instead it is deleting all new rows. LOG_TS is a timestamp but that should not matter since DATE returns just the date portion of a date or timestamp...right? I'm not a SQL guru but I can't see...
4
2481
by: hph | last post by:
Okay, another trivial matter that I can't solve. I have a variable - $lastdate - that is the latest date any record in a MySQL database was updated. Its MySQL format is TIMESTAMP. If I say I get the output I'd expect - 20060424221549 which is a YYYYMMDDHHMMSS format. I'd like to be able to display that using a format of mm/dd/yy with no leading spaces on the month and day. I don't know what the php command
3
1913
by: Tgone | last post by:
Hello, I need to convert a MySQL date "2007-03-08", into "March 8th, 2007". Normally I would use MySQL to accomplish this task but I have to use PHP. Here's my code: echo date("F jS, Y", $row);
3
3439
by: dave | last post by:
I need to compute an expiration date based on the number of hours, days, or months purchased. The expiration date needs to be expressed in minutes something like '1260481600'. How can I get the current date and time expressed in minutes? Once I have this number I can add the number of minutes purchased to the current date/time to get the expiration date.
4
1666
by: mtuller | last post by:
I have a database that stores time in 2 different formats. I have a unix timestamp, and the other is a representation of time in the format 03:00:00 which would represent 3 hrs. I want to convert the 3 hrs so that I can add it to the unix timestamp. In other words, The unix timestamp of 1183553400 is equal to Wed, Jul 04, 2007 7:50 AM. I want to add the 3hrs to be able to display in this format Wed, July 04, 2007 7:50 AM - 10:50 AM.
0
8666
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9145
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8880
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8853
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7703
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5857
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3033
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2319
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1992
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.