LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 11-13-2008, 01:21 AM   #1
NaCo
Member
 
Registered: Jun 2002
Location: L.A.
Distribution: Fedora
Posts: 46

Rep: Reputation: 15
PHP: formatting current date & time as: 2006-04-12 13:47:36 and substract 15 minutes


Hello community,

I want to format PHP date and time to:

2006-04-12 13:47:36

and then substract 15 minutes, so the final result will be:

2006-04-12 13:32:36


So far I got this to format the current date & time:
$current_time=date("Y-n-j G:m:s");

But now I am stuck trying to figure out how to substract 15 minutes.

I will apreciate any help.

Thank you,

NACO.
 
Old 11-13-2008, 02:27 AM   #2
NaCo
Member
 
Registered: Jun 2002
Location: L.A.
Distribution: Fedora
Posts: 46

Original Poster
Rep: Reputation: 15
Resolved.

Bingo!

Here is the code to get what I needed:
Code:
<?php

//Right now date and time. Format: 2008-11-13 3:12:33
echo '<b>Date and Time when this page was fetched from server:</b><br>'.date("Y-n-j G:i:s");

//15 minutes ago date and time: Format:2008-11-13 2:57:33 
$past_time = date('Y-n-j G:i:s', mktime(date('G'),date('i')-15,date('s'), date('m'), date('d'), date('Y')));
echo '<br><br><b>The Date and Time 15 minutes before this page was fetched from server is:</b><br>'.$past_time;

?>
Hope this helps anyone who will need it.

Good luck!
 
Old 11-13-2008, 04:42 AM   #3
sleddog
Member
 
Registered: Jan 2002
Location: Labrador, Canada
Distribution: CentOS, Debian
Posts: 182

Rep: Reputation: 35
You could also use:

Code:
$past_time = date('Y-n-j G:i:s', time()-900);
 
Old 11-13-2008, 10:19 AM   #4
NaCo
Member
 
Registered: Jun 2002
Location: L.A.
Distribution: Fedora
Posts: 46

Original Poster
Rep: Reputation: 15
Interesting

sleddog, yeap I think your way is easier.

It's all about Unix timestamp.

Interesting, very interesting.

thank you.
 
Old 11-13-2008, 07:31 PM   #5
jlinkels
LQ Guru
 
Registered: Oct 2003
Location: Bonaire, Leeuwarden
Distribution: Debian /Jessie/Stretch/Sid, Linux Mint DE
Posts: 5,191

Rep: Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039Reputation: 1039
I have been writing a fairly large PHP application all about date and time, time intervals, schedules and whatnot. I can advise you to convert the time to timestamp as soon as it enters your program, do your arithmetic, and only for presentation purposes convert it back to date-time. Really, almost operations are easier done in timestamps.

You should also consider to write your entire program using UTC, system clock UTC, and at the very last moment, just before presentation convert it to UTC.

My company HQ is based in Europe UTC+1 and subject to daylight saving, I am located in UTC-4, no daylight saving. We transfer schedules regularly, and we have major schedule change in the night changing from summer to wintertime or vice versa. I can tell you, better use UTC.

jlinkels
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Date & time as dir name Pyrate_02 Linux - General 6 03-18-2006 03:53 AM
Help With Date & Time Jude Lucien Linux - General 3 10-11-2005 02:46 PM
Newbie Help With Date & Time Jude Lucien Debian 2 10-11-2005 10:21 AM
Setting environment variable to current date/time nafan Programming 1 08-23-2004 09:04 PM
Appending current date and time to a file frankietomatoes Linux - General 5 11-18-2002 02:09 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 05:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration