Good morning, I have a problem that I do not know how to solve, through web-scraping I take data from the website where I work and create a ics calendar file with my shifts, it works almost always well, only when I have a shift that ends at midnight or later I get an error, this is because in the page I find only the start date and not the end date, so if I finish at 00.00 the correct date should be the next day, but it is added the same as the beginning so I get an error, do you have any idea how to correct this error?
"python nameerror free variable referenced before assignment in enclosing scope"
# CLEANING DATETIME DATUM
datclnd = soup_datum.replace('.', ' ')
dated = tempo.datetime.strptime(datclnd, "%d %m %Y")
yearclnd = dated.year
monthclnd = dated.month
dayclnd = dated.day
# CLEANING DATETIME DATUM
# CLEANING DATETIME HOUR AND MINUTES
time_split = soup_dienstbegin.split(":")
dienstBH = int(time_split[0])
dienstBM = int(time_split[1])
#dienstende
time_split2 = soup_dienstende.split(":")
dienstEH = int(time_split2[0])
try:
dienstEM = int(time_split2[1])
except:
NOTHING
def ics_working():
event = Event()
event['uid'] = f'19970610T172345Z-AF23B2#{dayclnd}{monthclnd}{yearclnd}'
event.add('summary', f'{string1}{tagesinfo2} {soup_kommentar2} {kursinfo}')
event.add('description', f'{schiffinfo}Schichtdauer: {soup_schichtdauer}, Bezahlte Zeit: {soup_bezahltezeit}\n, Infos: {schiffinfo}')
event.add('dtstart', datetime(yearclnd,monthclnd,dayclnd,dienstBH,dienstBM,0))
event.add('dtend', datetime(yearclnd,monthclnd,dayclnd,dienstEH,dienstEM,0))
event.add('dtstamp', datetime(yearclnd,monthclnd,dayclnd,dienstBH,dienstBM,0))
organizer = vCalAddress(f'MAILTO:{receiver_email}')
organizer.params['cn'] = vText(f'{username} Monatsplan')
organizer.params['role'] = vText(f'{username} Monatsplan')
event['organizer'] = organizer
event['location'] = vText('Werftestrasse 5, 6002 Luzern')
# Adding events to calendar
cal.add_component(event)
# Printing working day info
print(f'Datum: {soup_datum} Dienst: {string1}{tagesinfo2} --> Mannschaft: {crew_list2} --> OK')
ics_working()
I want to get difference between a date that I found from the database and the other is the current date both are in the same format(example - 2018-06-09 11:02:49).
How can I find the difference between two dates in days format in CodeIgniter?
Try this
$date1 = date_create("2018-06-09 11:02:49");
$date2 = date_create("2018-06-08 11:02:49");
$diff = date_diff($date1, $date2);
echo $diff->format("%a day");
Hope this will help you :
First way : you can use CI date_helper's timespan method
$expired_date = '2018-06-09 11:02:49';
$post_date = strtotime($expired_date);
$now = time();
$units =1;
echo timespan($post_date, $now, $units);
Ouptput :
2 Days
Second way use core php date_diff() method
working demo : https://eval.in/1018721
//$expired_date = $query->row()->expired_at;
$expired_date = '2018-06-09 11:02:49';
$expired_date = date_create($expired_date);
$cdate = date_create('now');
$interval = date_diff($expired_date,$cdate);
//echo $interval->format('%a').PHP_EOL;
echo $interval->d;
Output :
2
CI reference : https://www.codeigniter.com/user_guide/helpers/date_helper.html
For more : http://php.net/manual/en/function.date-diff.php
If you're using PHP 5.3 >, this is by far the most accurate way of calculating the difference:
$earlier = new DateTime("2018-05-20 11:02:49");
$later = new DateTime("2018-06-11 11:02:49");
$diff = $later->diff($earlier)->format("%a day(s)");
I try to generate a list of days with their week number (defined by ISO 8601) accordingly :
mydate='2012-12-25 02:26:55.983'
for (i=1;i<365;i++)
{
mydateAsDate=new Date().parse('yyyy-MM-dd H:mm:ss.S',mydate)+i;
println 'Week ' + mydateAsDate.format('w') + ' => ' + mydateAsDate.format('dd.MM.yyyy');
}
This works but I would like to get the year also like this:
Week 1-2013
I can't figure out which year information I should take.
Any idea?
As Jon Skeet said, I'd recommend using Joda-Time.
If you do, the following should fix your issues:
mydate= new DateTime(2012,12,25)
yearLater = myDate.plusYears(1)
while(myDate < yearLater){
println "Week ${myDate.weekOfWeekyear} - ${myDate.year}"
myDate = myDate.plusDays(1)
}
Not sure I understand, but you mean like:
String startDateString = '2012-12-25 02:26:55.983'
Date startDate = Date.parse( 'yyyy-MM-dd H:mm:ss.S', startDateString )
(1..364).each { i ->
println( (startDate++).format( "dd.MM.yyyy : 'Week' w'-'yyyy" ) )
}
I got it : SimpleDateFormat delivers the right week year information when using the YYYY format
thus this is only available in java 1.7
thanks for your responses though !
cheers
I need to get the end date of the given month for some calculation purpose,
how can I do that in PHP, I tried using date() function, but It didn't work.
I used this:
date($year.'-'.$month.'-t');
But this gives the current month's end date.
I think I'm wrong somewhere, I couldn't find where I'm going wrong here.
If I give year as 2012 & month as 03, then it must show me as 2012-03-31.
This code will give you last day for a specific month.
$datetocheck = "2012-03-01";
$lastday = date('t',strtotime($datetocheck));
You want to replace your date() call with:
date('Y-m-t', strtotime($year.'-'.$month.'-01'));
The first parameter to date() is the format you want to be returned, and the second parameter has to be a unix timestamp (or not passed to use the current timestamp). In your case, you can generate a timestamp with the function strtotime(), passing it a date string with the year, the month, and 01 for the day. It will return that same year and month, but the -t in the format will be replaced by the last day of the month.
If you want to return only the last day of the month without year and month:
date('t', strtotime($year.'-'.$month.'-01'));
Just use 't' as your format string.
Current month:
echo date('Y-m-t');
Any month:
echo date('Y-m-t', strtotime("$year-$month-1"));
Try below code.
$m = '03';//
$y = '2012'; //
$first_date = date('Y-m-d',mktime(0, 0, 0, $m , 1, $y));
$last_day = date('t',strtotime($first_date));
$last_date = date('Y-m-d',mktime(0, 0, 0, $m ,$last_day, $y));
function lastday($month = '', $year = '') {
if (empty($month)) {
$month = date('m');
}
if (empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 month', $result));
return date('Y-m-d', $result);
}
function firstOfMonth() {
return date("Y-m-d", strtotime(date('m').'/01/'.date('Y').' 00:00:00')). 'T00:00:00';}
function lastOfMonth() {
return date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')))). 'T23:59:59';}
$date1 = firstOfMonth();
$date2 = lastOfMonth();
try this ,this give you a current month's starting and ending date.
date("Y-m-d",strtotime("-1 day" ,strtotime("+1 month",strtotime(date("m")."-01-".date("Y")))));
function getEndDate($year, $month)
{
$day = array(1=>31,2=>28,3=>31,4=>30,5=>31,6=>30,7=>31,8=>31,9=>30,10=>31,11=>30,12=>31);
if($year%100 == 0)
{
if($year%400 == 0)
$day[$month] = 29;
}
else if($year%4 == 0)
$day[$month] = 29;
return "{$year}-{$month}-{$day[$month]}";
}
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$a_date = "2012-03-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');
I need to make an PHP operation with dates with format ISO 8601. Something like:
$starDate = 2012-03-20T00:00:00+01:00; //20 March 2012
$endDate = 2012-04-01T00:00:00+02:00; // 1 April 2012
$diff = $starDate - $endDate; //Result should be: 13
Using this code $diff get a value of cero.
Try this :
function date_diff($date1, $date2)
{
$s = strtotime($date2)-strtotime($date1);
$d = intval($s/86400)+1;
return "$d";
}
Source : http://forum.hardware.fr/hfr/Programmation/PHP/php-calculer-nombre-sujet_30415_1.htm