LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-10-2010, 04:22 AM   #1
delta function
Member
 
Registered: Jul 2004
Posts: 51

Rep: Reputation: 20
PHP and MYSQL Timestamps


Hi,
I am currently working on a project where instead of normal dates, they work with week schedules.

So i need to extract the week of any given date from mysql timestamp.

I know it can be done with mktime(0,0,0,Y,M,D), but how to insert the data with PHP ?

Example:

The timestamp MYSQL is: 2010-08-27

Question: how can I use PHP to extract the week of any given date in mysql timestamp form ????

Found some examples but they did not work, or I am overlooking something very very simple here. Can someone help me out ??

Thanks

Last edited by delta function; 07-10-2010 at 04:49 AM. Reason: [SOLVED]
 
Old 07-10-2010, 04:49 AM   #2
delta function
Member
 
Registered: Jul 2004
Posts: 51

Original Poster
Rep: Reputation: 20
[SOLVED]

$given_date = 2010-08-27 \\ UNIX timestamp

list($yr,$mn,$dt) = split('-',$given_date); // split the given date in year,month and day

$week=date("W",mktime(0,0,0,$mn,$dt,$yr)); // insert the variables

voila, the week of any given day
 
Old 07-10-2010, 07:29 AM   #3
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,195

Rep: Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043Reputation: 1043
Note that timestamp usually refers to the Unix time stamp, or the number of seconds since the epoch (1970-01-01 00:00).

Your method is not wrong, but perhaps a slightly easier way is this:
Retrieve the MySQL time as a real timestamp: SELECT UNIX_TIMESTAMP(yourdate).

yourdate is the mysql date value or datetime value from your table. This returns a large integer, let it be $ts. This large integer can be passed to strftime:

PHP Code:
$weeknr=strftime('W'$ts); 
jlinkels
 
Old 07-11-2010, 04:08 AM   #4
delta function
Member
 
Registered: Jul 2004
Posts: 51

Original Poster
Rep: Reputation: 20
Thanks for your reply.

One thing is odd. I tried strfftime() but got the wrong week number all along. But after checking some of my test functions, i found that I did not use the SELECT UNIX_TIMESTAMP method.

As I already said, I simply overlooked this one.

Many thanks, your post did made my life easier
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
php/shell script to install mysql,apache and php automatically stranger_6_7 Linux - General 2 08-11-2009 02:07 AM
php log file that traces php to mysql connections troubles ! mehdi1973 Linux - Software 12 07-14-2009 01:50 AM
yum install php-mysql fails with mysql 5.1 - "Error: mysql conflicts with MySQL" rebelde Linux - Software 2 03-13-2009 10:32 AM
Problem getting PHP to recognize MySQL, Using PHP 4.0 and MySQL 4.0.20 d2army Programming 4 06-27-2004 08:54 PM
Apache Mysql Php: mysql with php doesn't work breakerfall Linux - Networking 6 12-27-2003 08:59 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration