Sponsored Content
Top Forums Shell Programming and Scripting How to calculate specific hours between 2 dates Post 302218503 by sickboy on Friday 25th of July 2008 10:35:32 AM
Old 07-25-2008
Good one, but actually I don't want to take it so far. The only thing I will do is to subtract the weekends. I found a really interesting article how to find how many weekends are between two dates. http://dirtsimple.org/2004/10/nineteen-hundred-weekends.html

So what I thought so far is:
I have the startdate and the closing date.
I wrote this to adjust the startdate to working hours

Code:
    if (startdate.day_of_week in (5,6)) or (startdate.day_of_week == 4 and startdate.hour > 18):
        print 'I am in the first if'
        while True:
            startdate = startdate + mx.DateTime.RelativeDateTime(days=+1)
            print startdate.day_of_week
            if startdate.day_of_week == 0:
                startdate = startdate + mx.DateTime.RelativeDateTime(hour=9,minute=0,second=0)
                break
    elif startdate.day_of_week in (0,1,2,3,4) and startdate.hour < 9:
        startdate = startdate + mx.DateTime.RelativeDateTime(hour=9,minute=0,second=0)
    elif startdate.day_of_week in (0,1,2,3) and startdate.hour > 18:
        startdate = startdate + mx.DateTime.RelativeDateTime(days=+1,hour=9,minute=0,second=0)

but this is only if the startdate is out of the working hours.
It becomes harder when the starting date is for example 17:00. Then I have to keep this 1hour in a temp value calculate for the closing date and add it at the end.

Edit:
If the startdate is within the working times I guess I just have to subtract 15 hours

Last edited by sickboy; 07-25-2008 at 11:50 AM..
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Calculate days between yyyyMmmdd dates on Solaris

I extract dates from the log file and need to calculate days between two dates. My dates are in yyyyMmmdd format. Example: $d1=2011 Oct 21 $d2=2012 Feb 20 I need to calculate the number of days between $d2 and $d1. This is on Solaris. Any ideas? Thanks, djanu (4 Replies)
Discussion started by: djanu
4 Replies

2. Web Development

Calculate the number of days between 2 dates - PHP

Is this code good for this purpose? <?php $date1 = mktime(0,0,0,01,01,1991); $date2 = mktime(0,0,0,03,22,2012); $diff = $date2 - $date1; $days = $diff / (60*60*24); echo ($days . "<br />"); ?> (3 Replies)
Discussion started by: kovacsakos
3 Replies

3. Shell Programming and Scripting

Calculate the number of days between 2 dates - bash script

I wrote the day calculator also in bash. I would like to now, that is it good so? #!/bin/bash datum1=`date -d "1991/1/1" "+%s"` datum2=`date "+%s"` diff=$(($datum2-$datum1)) days=$(($diff/(60*60*24))) echo $days Thanks in advance for your help! (3 Replies)
Discussion started by: kovacsakos
3 Replies

4. Shell Programming and Scripting

How to Calculate the difference between two dates?

I want the difference between two following date using scripts in terms of no.of days. How I can accomplish this. lastdate=Tue Nov 13 10:30:56 2012 currdate=Wed Dec 15 15:58:21 PAKST 2012 Ouput should be like this: Your Password will expire after = 32 Days on Wed Dec 15 15:58:21 PAKST... (1 Reply)
Discussion started by: m_raheelahmed
1 Replies

5. Shell Programming and Scripting

Shell script to calculate difference between 2 dates

shell script to calculate difference between 2 dates (3 Replies)
Discussion started by: gredpurushottam
3 Replies

6. Shell Programming and Scripting

Calculate time difference between pst and pdt dates in perl

Hi, how to calculate the time difference between PST date and PDT date in perl scripting. date1: Mon Dec 31 16:00:01 PST 2015 date2: Tue Mar 19 06:09:30 PDT 2013 and also difference between PST-PST and PDT-PDT need difference in months or days (months prefereble). (3 Replies)
Discussion started by: praveen265
3 Replies

7. Shell Programming and Scripting

Collect files for specific hours

I have to fetch files from a location hour wise. Eg files available at location /tmp/data/ are A20140205.1300-1315...... . . A20140205.1400-1415...... . . A20140205.1700-1715...... . . . . Below is the code I have prepared. But it works only for one hour. For instance... (1 Reply)
Discussion started by: Saidul
1 Replies

8. UNIX for Advanced & Expert Users

How to get ALL update dates and hours of a file?

Hello, I have a question please, How could I get ALL update dates and hours of a file ? For example: I modified a file on 09/04/2014 at 7:am for the first time. Then, I modified again the same file on 09/04/2014 at 9 am and finally, I modified the same file again on 10/04/2014 at... (3 Replies)
Discussion started by: nurinolo
3 Replies

9. Shell Programming and Scripting

Find hours difference between two dates in given format

I have two dates in below format, how would I find the hours difference between the two dates. Im using AIX and ksh. Current date : Wed May 17 14:34:41 SGT 2017 File date : Thu Apr 27 20:52:41 SGT 2017 (3 Replies)
Discussion started by: simpltyansh
3 Replies

10. Red Hat

Delete files older than 1week(dates need to be calculate based on file name)

Objective: We have multiple files in a folder and we want to delete all files except for last 1 week files. Note: We are copying these files from original location to this temporary location. So time shown for these files are time when we copied to this location. Not that when file was created.... (2 Replies)
Discussion started by: Agoyals1986
2 Replies
DB2_EXEC(3)								 1							       DB2_EXEC(3)

db2_exec - Executes an SQL statement directly

SYNOPSIS
resource db2_exec (resource $connection, string $statement, [array $options]) DESCRIPTION
Executes an SQL statement directly. If you plan to interpolate PHP variables into the SQL statement, understand that this is one of the more common security exposures. Con- sider calling db2_prepare(3) to prepare an SQL statement with parameter markers for input values. Then you can call db2_execute(3) to pass in the input values and avoid SQL injection attacks. If you plan to repeatedly issue the same SQL statement with different parameters, consider calling db2_prepare(3) and db2_execute(3) to enable the database server to reuse its access plan and increase the efficiency of your database access. PARAMETERS
o $connection - A valid database connection resource variable as returned from db2_connect(3) or db2_pconnect(3). o $statement - An SQL statement. The statement cannot contain any parameter markers. o $options - An associative array containing statement options. You can use this parameter to request a scrollable cursor on database servers that support this functionality. For a description of valid statement options, see db2_set_option(3). RETURN VALUES
Returns a statement resource if the SQL statement was issued successfully, or FALSE if the database failed to execute the SQL statement. EXAMPLES
Example #1 Creating a table with db2_exec(3) The following example uses db2_exec(3) to issue a set of DDL statements in the process of creating a table. <?php $conn = db2_connect($database, $user, $password); // Create the test table $create = 'CREATE TABLE animals (id INTEGER, breed VARCHAR(32), name CHAR(16), weight DECIMAL(7,2))'; $result = db2_exec($conn, $create); if ($result) { print "Successfully created the table. "; } // Populate the test table $animals = array( array(0, 'cat', 'Pook', 3.2), array(1, 'dog', 'Peaches', 12.3), array(2, 'horse', 'Smarty', 350.0), array(3, 'gold fish', 'Bubbles', 0.1), array(4, 'budgerigar', 'Gizmo', 0.2), array(5, 'goat', 'Rickety Ride', 9.7), array(6, 'llama', 'Sweater', 150) ); foreach ($animals as $animal) { $rc = db2_exec($conn, "INSERT INTO animals (id, breed, name, weight) VALUES ({$animal[0]}, '{$animal[1]}', '{$animal[2]}', {$animal[3]})"); if ($rc) { print "Insert... "; } } ?> The above example will output: Successfully created the table. Insert... Insert... Insert... Insert... Insert... Insert... Insert... Example #2 Executing a SELECT statement with a scrollable cursor The following example demonstrates how to request a scrollable cursor for an SQL statement issued by db2_exec(3). <?php $conn = db2_connect($database, $user, $password); $sql = "SELECT name FROM animals WHERE weight < 10.0 ORDER BY name"; if ($conn) { require_once('prepare.inc'); $stmt = db2_exec($conn, $sql, array('cursor' => DB2_SCROLLABLE)); while ($row = db2_fetch_array($stmt)) { print "$row[0] "; } } ?> The above example will output: Bubbles Gizmo Pook Rickety Ride Example #3 Returning XML data as an SQL ResultSet The following example demonstrates how to work with documents stored in a XML column using the SAMPLE database. Using some pretty simple SQL/XML, this example returns some of the nodes in a XML document in an SQL ResultSet format that most users are familiar with. <?php $conn = db2_connect("SAMPLE", "db2inst1", "ibmdb2"); $query = 'SELECT * FROM XMLTABLE( XMLNAMESPACES (DEFAULT 'http://posample.org'), 'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo' COLUMNS "CID" VARCHAR(50) PATH '@Cid', "NAME" VARCHAR(50) PATH 'name', "PHONE" VARCHAR(50) PATH 'phone [ @type = "work"]' ) AS T WHERE NAME = 'Kathy Smith' '; $stmt = db2_exec($conn, $query); while($row = db2_fetch_object($stmt)){ printf("$row->CID $row->NAME $row->PHONE "); } db2_close($conn); ?> The above example will output: 1000 Kathy Smith 416-555-1358 1001 Kathy Smith 905-555-7258 Example #4 Performing a "JOIN" with XML data The following example works with documents stored in 2 different XML columns in the SAMPLE database. It creates 2 temporary tables from the XML documents from 2 different columns and returns an SQL ResultSet with information regarding shipping status for the cus- tomer. <?php $conn = db2_connect("SAMPLE", "db2inst1", "ibmdb2"); $query = ' SELECT A.CID, A.NAME, A.PHONE, C.PONUM, C.STATUS FROM XMLTABLE( XMLNAMESPACES (DEFAULT 'http://posample.org'), 'db2-fn:xmlcolumn("CUSTOMER.INFO")/customerinfo' COLUMNS "CID" BIGINT PATH '@Cid', "NAME" VARCHAR(50) PATH 'name', "PHONE" VARCHAR(50) PATH 'phone [ @type = "work"]' ) as A, PURCHASEORDER AS B, XMLTABLE ( XMLNAMESPACES (DEFAULT 'http://posample.org'), 'db2-fn:xmlcolumn("PURCHASEORDER.PORDER")/PurchaseOrder' COLUMNS "PONUM" BIGINT PATH '@PoNum', "STATUS" VARCHAR(50) PATH '@Status' ) as C WHERE A.CID = B.CUSTID AND B.POID = C.PONUM AND A.NAME = 'Kathy Smith' $stmt = db2_exec($conn, $query); while($row = db2_fetch_object($stmt)){ printf("$row->CID $row->NAME $row->PHONE $row->PONUM $row->STATUS "); } db2_close($conn); ?> The above example will output: 1001 Kathy Smith 905-555-7258 5002 Shipped Example #5 Returning SQL data as part of a larger XML document The following example works with a portion of the PRODUCT.DESCRIPTION documents in the SAMPLE database. It creates a XML document containing product description (XML data) and pricing info (SQL data). <?php $conn = db2_connect("SAMPLE", "db2inst1", "ibmdb2"); $query = ' SELECT XMLSERIALIZE( XMLQUERY(' declare boundary-space strip; declare default element namespace "http://posample.org"; <promoList> { for $prod in $doc/product where $prod/description/price < 10.00 order by $prod/description/price ascending return( <promoitem> { $prod, <startdate> {$start} </startdate>, <enddate> {$end} </enddate>, <promoprice> {$promo} </promoprice> } </promoitem> ) } </promoList> ' passing by ref DESCRIPTION AS "doc", PROMOSTART as "start", PROMOEND as "end", PROMOPRICE as "promo" RETURNING SEQUENCE) AS CLOB(32000)) AS NEW_PRODUCT_INFO FROM PRODUCT WHERE PID = '100-100-01' $stmt = db2_exec($conn, $query); while($row = db2_fetch_array($stmt)){ printf("$row[0] "); } db2_close($conn); ?> The above example will output: <promoList xmlns="http://posample.org"> <promoitem> <product pid="100-100-01"> <description> <name>Snow Shovel, Basic 22 inch</name> <details>Basic Snow Shovel, 22 inches wide, straight handle with D-Grip</details> <price>9.99</price> <weight>1 kg</weight> </description> </product> <startdate>2004-11-19</startdate> <enddate>2004-12-19</enddate> <promoprice>7.25</promoprice> </promoitem> </promoList> SEE ALSO
db2_execute(3), db2_prepare(3). PHP Documentation Group DB2_EXEC(3)
All times are GMT -4. The time now is 01:29 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy