-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprocessAuction.php
97 lines (81 loc) · 2.42 KB
/
processAuction.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
header('Content-Type:text/xml');
?>
<?php
$xmlFile = "../../data/auction.xml";
if(file_exists($xmlFile))
{
fnLoadValidItems($xmlFile);
}
else
{
ECHO "No items are listed for auction";
}
function fnLoadValidItems($xmlFile)
{
$count = 0;
$doc = DOMDocument::load($xmlFile);
$items = $doc->getElementsByTagName("item");
foreach($items as $item)
{
$Status = $item->getElementsByTagName("Status");
$Status = $Status->item(0)->nodeValue;
if($Status == "in-progress")
{
$Duration = $item->getElementsByTagName("Duration");
$Duration = $Duration->item(0)->nodeValue;
$StartDate = $item->getElementsByTagName("StartDate");
$StartDate = $StartDate->item(0)->nodeValue;
$StartTime = $item->getElementsByTagName("StartTime");
$StartTime = $StartTime->item(0)->nodeValue;
$TimeLeft = fnCalculateTimeLeft($Duration,$StartDate,$StartTime);
if($TimeLeft == -1)
{
$count = $count + 1;
$CurrentBidPrice = $item->getElementsByTagName("CurrentBidPrice");
$CurrentBidPrice = $CurrentBidPrice->item(0)->nodeValue;
$ReservePrice = $item->getElementsByTagName("ReservePrice");
$ReservePrice = $ReservePrice->item(0)->nodeValue;
if($CurrentBidPrice >= $ReservePrice)
{
$Status = $item->getElementsByTagName("Status");
$Status->item(0)->nodeValue = "sold";
}
else
{
$Status = $item->getElementsByTagName("Status");
$Status->item(0)->nodeValue = "failed";
}
$doc->saveXML();
}
}
}
$doc->save($xmlFile);
if($count > 0)
echo $count." Auction items processed successfully";
else
echo "No Auctions items processed";
}
function fnCalculateTimeLeft($Duration,$StartDate,$StartTime)
{
//$StartDate ="YYYY-MM-DD";
//$StartTime ="HH:MM";
//$Duration = "Day:Hours:Mins";
//Concatenate the date and time provided
$dt = $StartDate." ".$StartTime;
//Create a date format using
$Date = new DateTime($dt);
//Convert duration into Min
$arrConverter = explode(":",$Duration);
$DurationMin = $arrConverter[0]*24*60;
$DurationMin = $DurationMin + ($arrConverter[1]*60) + ($arrConverter[2]);
$EndDate = $Date->add(new DateInterval('PT' . $DurationMin . 'M'));
$now = new DateTime();
$interval = $EndDate->diff($now);
//ECHO "interval:".$interval->format('%d Day : %H Hours: %i Min');
if(strtotime(date_format($EndDate,'Y-m-j H:i')) > time())
return $interval->format('%d Day : %H Hours: %i Min');
else
return -1;
}
?>