How can I add hours as well as subtract minutes to get one result?
I can add 4 hours using the following code:
//Current Time
$hourMin = date('H:i:s');
echo "Current time is: ". $hourMin."<br>";
$hourDiff = date('H:i:s', time()+14400000);
echo "New time is: " . $hourDiff;
I can subtract 7 minutes using the following code:
//Current Time
$hourMin = date('H:i:s');
echo "Current time is: ". $hourMin."<br>";
$hourDiff = date('H:i:s', time()-420000);
echo "New time is: " . $hourDiff;
How do I do this so that my time in variable $hourDiff is 4 hours ahead but 7 minutes behind?
$now = date("H:i:s", time());
$newTime = strtotime("$now +4 hours -7 minutes");
or
$newTime = strtotime("$now +3 hours +53 minutes");
echo "New time is: " . date("H:i:s", $newTime);
something like this?
Related
I am trying to write a simple script that helps me estimate the time left in an entry queue. So far I have gotten this, but it seems to deliver entirely random results, especially as the time in queue gets longer, or the position gets closer to zero.
To clarify, the script prompts for the initial position 'inum' whenever the queue moves, the user is prompted to enter the current place in queue. (There is no constant movement) I wanted to calculate the time until entry by taking the current place in queue, comparing it to the total amount of people, and then use the elapsed time in queue as a means to estimate a remaining time (+ a 5 Minute grace period). However, this gives me very inconsistent results.
#!/bin/sh
read -p "Users in queue: " inum;
echo ""
n=$inum
t0=$(date +%s)
while true; do
read -p "Users in queue: " nleft;
t=$(date +%s)
d=$((t - t0))
cal=$(echo "((($d * $nleft / ($n - $nleft)) - $d) / 60) + 5" | bc)
echo "Time in queue: $((d / 60)) Minutes."
echo "Approximate time left: $cal Minutes."
echo ""
done
echo "End."
with time command, use
time yourscript.sh
or with
start=`date +%s`
read -p "Users in queue: " nleft; #into while
end=`date +%s`
caltime=$( echo "$end - $start" | bc -l )
I have created two functions in the Powershell script as below.
$date = Get-date
Write-Output $date.ToString("dd_MM_yyyy")
Function Oneday{
Param ($Name)
Write-Output "This funtion will run everyday"
}
Function Fiveday{
Param ($Name)
Write-Output "This funtion will run every 5 days"
}
My goal is to put this PowerShell script as a task scheduler and run the function 'Oneday' every day and function 'Fiveday' every 5 days. I tried to think of many ways by using function AddDays and all but couldn't get how can I achieve this task.
Use the modulo/remainder operator (%) on the value of the DayOfYear property of the current date to only run on every 5th day:
$date = Get-date
# Run every day
Oneday
if($date.DayOfYear % 5 -eq 0){
# Only every 5th day
Fiveday
}
To carry over multiple years, anchor against a starting date in the past:
$start = Get-Date 2020-01-01
$date = Get-date
# Run every day
Oneday
if(($date - $start).Days % 5 -eq 0){
# Only every 5th day
Fiveday
}
I am doing an assignment for Powershell and one of the functions is to say when the last boot was. I am printing date and 'time since', date works fine but I think there is too much code for displaying the 'time since'. I want the first value to not be zero. Like this:
1 Hour, 0 Minutes, 34 Seconds
and not like this:
0 Days, 1 Hours, 0 Minutes, 34 Seconds
$bootDate = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$bootTime = $(Get-Date).Subtract($bootDate)
# Think there is an easier way, but couldn't find any :/
$time = ""
if($bootTime.Days -ne 0) {
$time = "$($bootTime.Days) Days, $($bootTime.Hours) Hours, $($bootTime.Minutes) Minutes, "
} elseif($bootTime.Hours -ne 0){
$time = "$($bootTime.Hours) Hours, $($bootTime.Minutes) Minutes, "
} elseif($bootTime.Minutes -ne 0){
$time = "$($bootTime.Minutes) Minutes, "
}
echo "Time since last boot: $time$($bootTime.Seconds) Seconds"
echo "Date and time: $($bootDate.DateTime)"
This code prints it as I want it to be, but is just seems like too much code for something so little. Is there an easier way?
Make sure you inspect to TotalDays rather than Days. Additionally, I would split the code into a separate function:
function Get-TruncatedTimeSpan {
param([timespan]$TimeSpan)
$time = ""
if($TimeSpan.TotalDays -ge 1) {
$time += "$($TimeSpan.Days) Days, "
}
if($TimeSpan.TotalHours -ge 1){
$time += "$($TimeSpan.Hours) Hours, "
}
if($TimeSpan.TotalMinutes -ge 1){
$time += "$($TimeSpan.Minutes) Minutes, "
}
return "$time$($TimeSpan.Seconds) Seconds"
}
$bootDate = (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
$bootTime = $(Get-Date).Subtract($bootDate)
echo "Time since last boot: $(Get-TruncatedTimeSpan $bootTime)"
echo "Date and time: $($bootDate.DateTime)"
A concise solution based on removing the longest run of 0-valued components from the start, using the -replace operator, which uses a regular expression for matching (and by not specifying a replacement string effectively removes the match):
function get-FriendlyTimespan {
param([timespan] $TimeSpan)
"{0} Days, {1} Hours, {2} Minutes, {3} Seconds" -f
$TimeSpan.Days, $TimeSpan.Hours, $TimeSpan.Minutes, $TimeSpan.Seconds -replace
'^0 Days, (0 Hours, (0 Minutes, )?)?'
}
# Invoke with sample values (using string-based initialization shortcuts):
"0:0:1", "0:1:0", "1:0:0", "1", "0:2:33" | % { get-FriendlyTimespan $_ }
The above yields:
1 Seconds
1 Minutes, 0 Seconds
1 Hours, 0 Minutes, 0 Seconds
1 Days, 0 Hours, 0 Minutes, 0 Seconds
2 Minutes, 33 Seconds
I have been working on this on and off for the last two months, and despite how many times I look at it, I can't make it work.
This script checks daily log files for a user defined variable, so they don't have to look through every one manually. It worked great checking the current month, but if the user wants to check back 20 days, and today is the 12th of this month, I wanted to be able to then go back to the previous month (not look for the log file with a date of 20150399 and so on). I have checked the logic for my date/day computations, and they seem okay (if there is a better way to do that in BASH, I am open to suggestions). What happens when I try to debug is unexpected end of file. I am somewhat new to writing scripts that contain more than 20 or so lines, but I just can't come up with what I am missing.
I have tried various fixes, to no avail, but I think this is the last iteration.
Ideas?
#!/bin/bash
########################################################
# multi_log_chk.sh
# This script will take input from the user and report which
# CyberFusion MFT logs contain what the user is looking for.
# Hopefully this will save the user having to search through every
# stinking log file to find what they are looking for.
# 20150406 pxg007 started typing
# 20150413 pxg007 added && comparison for back out (line 28)
# added message for no entries found (line 32, 38, 48-52)
# Added some further description (line 16)
# 20150424 pxg007 Added logic to calculate previous month and if necessary, year. (Lines 16-24, 60-78 )
#
########################################################
currDate=`date +%d%B%C%y`
currDay=`date +%d`
currMnth=`date +%m`
currYear=`date +%C%y`
case $currMnth in #Let's establish number of days for previous month
05 | 07 | 10 | 12 ) lastMnthD=30;;
01 |02 | 04 | 06 | 09 | 08 | 11 ) lastMnthD=31;;
03 ) lastMnthD=28;; ##and screw leap year
esac
if [ $currMnth -eq 01 ]; then ##accounting for January
lastMnth=12
else
lastMnth=$((currMnth-1))
fi
if [ $lastMnth -eq 12 ]; then ## accounting for Dec of previous year
lastMnthYr=$((currYear-1))
else
lastMnthYr=$currYear
fi
echo "This script will find entries for your query in whatever available MFT logs you request."
echo " "
echo "For instance - how many log files have transfer entries with \"DOG\" in them?"
echo " "
echo "I also will also give an estimate of how many transfers per log file contain your query, give or take a couple."
echo " "
echo "This search is case sensitive, so \"DOG\" is *** NOT *** the same as \"dog\""
echo " "
read -p "What text you are looking for? Punctuation is okay, but no spaces please. " looking ### what we want to find
echo " "
echo "Today's date is: $currDate."
echo " "
read -p "How many days back do you want to search(up to 25)? " daysBack ### How far back we are going to look
if [ "$daysBack" == 0 ] && [ "$daysBack" >> 25 ]; then
echo "I said up to 25 days. We ain't got more than that!"
exit 1
fi
echo " "
echo "I am going to search through the last $daysBack days of log files for:\"$looking\" "
echo " "
read -p "Does this look right? Press N to quit, or any other key to continue: " affirm
if [ "$affirm" = N ] && [ "$affirm" = n ]; then ###Yes, anything other than "N" or "n" is a go
echo "Quitter!"
exit 1
else
nada=0 ### Used to test for finding anything
backDate=$((currDay-daysBack)) ### current month iterator (assuming query covers only current month)
if (("$daysBack" => "$currDay")); then ## If there are more logs requested than days in the month...
lastMnthCnt=$((daysBack-currDay)) ### how many days to check last month
lastMnthStrt=$((lastMnthD-lastMnthCnt)) ## last month start and iterator
backDate=$(currDay-(daysBack-lastMnthCnt)) # Setting the iterator if we have to go back a month
while (("$lastMnthStrt" <= "$lastMnthD" )); do
foundIt=$(grep "$looking" /CyberFusion/log/Log.txt."$lastMnthYr$lastMnth$lastMnthStrt" | parsecflog | wc -l )
howMany=$((foundIt/40+1)) ### Add one in case there are less than 40 lines in the record.
if (("$foundIt" > 0))
then
nada=$((nada+1))
echo "Log.txt.$lastMnthYr$lastMnth$lastMnthStrt contains $looking in approximately $howMany transfer records."
lastMnthStrt=$((lastMnthStrt+1))
echo " "
else
lastMnthStrt=$((lastMnthStrt+1))
fi
fi
backDate=$((currDay-daysBack)) ### current month iterator (assuming query covers only current month)
while (("$backDate" <= "$currDay")); do
foundIt=$(grep "$looking" /CyberFusion/log/Log.txt."$backDate" | parsecflog | wc -l )
howMany=$((foundIt/40+1)) ### Add one in case there are less than 40 lines in the record.
if (("$foundIt" > 0))
then
nada=$((nada+1))
echo "Log.txt.$backDate contains $looking in approximately $howMany transfer records."
backDate=$((backDate+1))
echo " "
else
backDate=$((backDate+1))
fi
if [ "$nada" \< 1 ]
then
echo " "
echo "I found no entries for $looking in any log file."
fi
You are missing the keyword 'done' on lines 81 and 96 and also a final 'fi' keyword on the last line.
Also as others suggested you can do
date -d "20 days ago" +"%d%B%C%y"
to easily get dates in the past
I need help using time in the c-shell
I want to know how much time it took to execute a script,so i want to do in the following way
1.set start_time=time
2 script part
3.set end_time=time
4. set diff=end_time-start_time
5.echo "It took $diff seconds"
but i couldn't get the time value using any command.
could any one suggest a command to read the time value in c-shell
I think you want the "date" command, with the format to give you raw seconds:
# start_time = `date +%s`
script part
# end_time = `date +%s`
# diff = $end_time - $start_time
echo "It took $diff seconds"