Adding time in php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

mio510
Forum Newbie
Posts: 12
Joined: Sat Jan 02, 2016 6:37 pm

Adding time in php

Post by mio510 »

Hi everyone

I'm trying to adjust the time of this widget for wordpress which is called Namaz Vakti. its a muslim prayer time widget you can check the widget page on this link https://wordpress.org/plugins/namaz-vakti/
There is also a github page for the widget at https://github.com/wp-plugins/namaz-vakti

There is a preview of the widget on the creators website which is http://www.erdemarslan.com/. if you look at the right sidebar you see the widget.

I'm using the widget on a local wordpress and its working fine without any problems.
i only need to know how i can adjust the time of the prayers. Like for example i want to add 30 minutes to the Fajir (imsak) prayer.

I did find this php file which is called widget.namazvakti.php and tried several things but without any luck.

Code: Select all

                </div>
            </div>
            
            <div class="vakitler">
            
            	<div id="imsak" class="vakit">
                	<div class="vakit_adi"><?php _e('İmsak', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['imsak']; ?></div>
                </div>
                
                <div id="gunes" class="vakit">
                	<div class="vakit_adi"><?php _e('Güneş', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['gunes']; ?></div>
                </div>
                
                <div id="ogle" class="vakit">
                	<div class="vakit_adi"><?php _e('Öğle', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['ogle']; ?></div>
                </div>
                
                <div id="ikindi" class="vakit">
                	<div class="vakit_adi"><?php _e('İkindi', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['ikindi']; ?></div>
                </div>
                
                <div id="aksam" class="vakit">
                	<div class="vakit_adi"><?php _e('Akşam', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['aksam']; ?></div>
                </div>
                
                <div id="yatsi" class="vakit">
                	<div class="vakit_adi"><?php _e('Yatsı', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['yatsi']; ?></div>
                </div>
                
                <div id="kible" class="vakit">
                	<div class="vakit_adi"><?php _e('Kıble', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['kiblesaati']; ?></div>
                </div>
            </div>
            </div>
        
        </div>
		<?php
		} else {
			echo 'İstenilen şehre ait veriler alınamadı!';
		}
		
		
		echo $after_widget;
	}
	
	
	private function hangi_vakitteyiz($vakitler)
	{
		$imsak	= strtotime( date('d.m.Y') . ' ' . $vakitler['imsak'] . ':00');
		$gunes	= strtotime( date('d.m.Y') . ' ' . $vakitler['gunes'] . ':00');
		$ogle	= strtotime( date('d.m.Y') . ' ' . $vakitler['ogle'] . ':00');
		$ikindi	= strtotime( date('d.m.Y') . ' ' . $vakitler['ikindi'] . ':00');
		$aksam	= strtotime( date('d.m.Y') . ' ' . $vakitler['aksam'] . ':00');
		$yatsi	= strtotime( date('d.m.Y') . ' ' . $vakitler['yatsi'] . ':00');
		
		$simdi = strtotime(date('d.m.Y H:i:s'));
				
		if ($simdi > $yatsi)
		{
			return 'yatsi';
		}
		elseif ($simdi <= $yatsi AND $simdi > $aksam)
		{
			return 'aksam';
		}
		elseif ($simdi <= $aksam AND $simdi > $ikindi)
		{
			return 'ikindi';
		}
		elseif ($simdi <= $ikindi AND $simdi > $ogle)
		{
			return 'ogle';
		}
		elseif ($simdi <= $ogle AND $simdi > $gunes)
		{
			return 'gunes';
		} else {
			return 'imsak';
		}
		
	}
	
	private function hicri_dil_duzeltmesi($tarih)
	{
		$exp = explode( ' ', $tarih );
		
		$hicri_ay = trim($exp[1]);
		
		return $exp[0] . ' ' . __($hicri_ay, 'namazvakti') . ' ' . $exp[2];
	}
}

found this in the end of the php file.
i tried

$imsak = strtotime( date('d.m.Y') . ' ' . $vakitler['imsak'] . ':00 + 30 minutes');

But it looks like the time is not even being affected.

Code: Select all

                </div>
            </div>
            
            <div class="vakitler">
            
            	<div id="imsak" class="vakit">
                	<div class="vakit_adi"><?php _e('İmsak', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['imsak']; ?></div>
                </div>
                
                <div id="gunes" class="vakit">
                	<div class="vakit_adi"><?php _e('Güneş', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['gunes']; ?></div>
                </div>
                
                <div id="ogle" class="vakit">
                	<div class="vakit_adi"><?php _e('Öğle', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['ogle']; ?></div>
                </div>
                
                <div id="ikindi" class="vakit">
                	<div class="vakit_adi"><?php _e('İkindi', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['ikindi']; ?></div>
                </div>
                
                <div id="aksam" class="vakit">
                	<div class="vakit_adi"><?php _e('Akşam', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['aksam']; ?></div>
                </div>
                
                <div id="yatsi" class="vakit">
                	<div class="vakit_adi"><?php _e('Yatsı', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['yatsi']; ?></div>
                </div>
                
                <div id="kible" class="vakit">
                	<div class="vakit_adi"><?php _e('Kıble', 'namazvakti'); ?></div>
                    <div class="vakit_saati"><?php echo $vakit['kiblesaati']; ?></div>
                </div>
            </div>
            </div>
        
        </div>
		<?php
		} else {
			echo 'İstenilen şehre ait veriler alınamadı!';
		}
		
		
		echo $after_widget;
	}
	
	
	private function hangi_vakitteyiz($vakitler)
	{
		$imsak	= strtotime( date('d.m.Y') . ' ' . $vakitler['imsak'] . ':00');
		$gunes	= strtotime( date('d.m.Y') . ' ' . $vakitler['gunes'] . ':00');
		$ogle	= strtotime( date('d.m.Y') . ' ' . $vakitler['ogle'] . ':00');
		$ikindi	= strtotime( date('d.m.Y') . ' ' . $vakitler['ikindi'] . ':00');
		$aksam	= strtotime( date('d.m.Y') . ' ' . $vakitler['aksam'] . ':00');
		$yatsi	= strtotime( date('d.m.Y') . ' ' . $vakitler['yatsi'] . ':00');
		
		$simdi = strtotime(date('d.m.Y H:i:s'));
				
		if ($simdi > $yatsi)
		{
			return 'yatsi';
		}
		elseif ($simdi <= $yatsi AND $simdi > $aksam)
		{
			return 'aksam';
		}
		elseif ($simdi <= $aksam AND $simdi > $ikindi)
		{
			return 'ikindi';
		}
		elseif ($simdi <= $ikindi AND $simdi > $ogle)
		{
			return 'ogle';
		}
		elseif ($simdi <= $ogle AND $simdi > $gunes)
		{
			return 'gunes';
		} else {
			return 'imsak';
		}
		
	}
	
	private function hicri_dil_duzeltmesi($tarih)
	{
		$exp = explode( ' ', $tarih );
		
		$hicri_ay = trim($exp[1]);
		
		return $exp[0] . ' ' . __($hicri_ay, 'namazvakti') . ' ' . $exp[2];
	}
}

Now i am not sure if i am editing the right file but if so please tell me which line of code i have to edit to add 30 minutes to the Imsak (fajir) prayer which is the first prayer in the table.
so all i want is a time offsets in minutes for each prayer time, in this case for the prayer Imsak (Fajir).

I also did emailed the creator of the widget a week ago but i have not received any mails back. The code is written in Turkish but there are still elemtns in the code in english.

Hope someone can help me out that would be great.

Thanks in advance
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding time in php

Post by requinix »

It's a bit more difficult than you might think to change the times.

Can I ask why? Are the times off for you? Should you be changing your location in the plugin's settings instead?
mio510
Forum Newbie
Posts: 12
Joined: Sat Jan 02, 2016 6:37 pm

Re: Adding time in php

Post by mio510 »

thanks for your reply

yes the time of the first prayer is off foor me by 30 minutes others are correct.
i changed the location in the setting but time is still wrong.

hope i can still add 30 minutes to the first prayer time.
any ideas i still could try?
mio510
Forum Newbie
Posts: 12
Joined: Sat Jan 02, 2016 6:37 pm

Re: Adding time in php

Post by mio510 »

one more question am i editing the right file?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding time in php

Post by requinix »

The times come from the Namaz class (class.namazvakti.php) - specifically the al_vakitler method. It is grabbing the times from a remote server. You'd need to modify the time at that point.

At the end of the method, $sonuc is an array and $sonuc[veri] contains the times. I think you can get away with putting a couple lines of code in there to alter the values as needed. First step is determining the exact values in use: it's kinda hard to do just by looking at code, especially for someone like me who can't read Turkish, so I would add a bit of code to dump out the values.
However that code needs to go somewhere else. While al_vakitler() grabs the values from the server, vakit() is the one that actually gets called by other code and will return the values. It also deals with the cache; first time you run the code it will go to the server, but for a time afterwards it will use the cache instead. Modify the end of the method to

Code: Select all

// line 194
$yazdir = $cikti == 'json' ? $sonuc : json_decode( $sonuc, TRUE );
print_r($yazdir);
return $yazdir;
run the code, and copy the output it creates - it'll look like "Array(...)".
mio510
Forum Newbie
Posts: 12
Joined: Sat Jan 02, 2016 6:37 pm

Re: Adding time in php

Post by mio510 »

thanks for the fast replies requinix

i just run the code with the above line you gave me and it has put out this

Code: Select all

Array ( [durum] => basarili [veri] => Array ( [yer_adi] => MAASTRICHT
[imsak] => 06:33 [gunes] => 08:35 [ogle] => 12:48 [ikindi] => 14:30 [aksam] => 16:49 [yatsi] => 18:09 [moonsrc] => http://www.diyanet.gov.tr/UserFiles/AyEvreleri/sd1.gif [hicritarih] => 23 Rebiü'l-Evvel 1437 [miladitarih] => 03.01.2016 [rumitarih] => [enlem] => 0 [boylam] => 0 [kibleacisi] => 125 [ulkeadi] => HOLLANDA [sehiradi] => HOLLANDA [kiblesaati] => 00:00 [gunesbatis] => 16:43 [gunesdogus] => 08:40 [itemid] => 13904 [holydaysitem] => ) ) 
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding time in php

Post by requinix »

The interesting part of that is

Code: Select all

[imsak] => 06:33 [gunes] => 08:35 [ogle] => 12:48 [ikindi] => 14:30 [aksam] => 16:49 [yatsi] => 18:09
I take it you want the "imsak" value to be 07:03 instead? What else?
mio510
Forum Newbie
Posts: 12
Joined: Sat Jan 02, 2016 6:37 pm

Re: Adding time in php

Post by mio510 »

i only want 30 minutes to be added to the imsak prayer thats the only thing i want for now.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding time in php

Post by requinix »

Okay. At the end of the al_vakitler method, just before it returns the data it got from the server, alter the imsak value. Easiest way is probably

Code: Select all

$yazdir["veri"]["imsak"] = date("H:i", strtotime($yazdir["veri"]["imsak"] . ":00 +30 minutes"));
which is basically the same thing that you were trying to do earlier; 'H' is a zero-padded 24-hour hour ("07") and 'i' is a zero-padded minute ("03").

Put that code in and then find and delete the existing cached data (I have no idea where that is).
mio510
Forum Newbie
Posts: 12
Joined: Sat Jan 02, 2016 6:37 pm

Re: Adding time in php

Post by mio510 »

Time has changed finally. you my man are a genius.

the only minor problem i have right now is when using the code for the time to be changed. i can not save other locations in the widget it self, so for example im changing from Germany Berlin to Holland Amsterdam the saving circle will always rotate and not go back to the prayers. What i can see is the change from a city to a different city has been done and saved but to actually see it i have to refresh my page to see the change. When im not using the time changing code all is back to normal and the changing locations id done immediately.

here is pic so you can see what i mean

Image
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding time in php

Post by requinix »

I can't imagine how that one line of code would change any behavior - all it does is update the single value. Other code should not have any idea that happened.

Did you make any other changes to the code besides that one line?
mio510
Forum Newbie
Posts: 12
Joined: Sat Jan 02, 2016 6:37 pm

Re: Adding time in php

Post by mio510 »

No i ave not changed any other code. i even deleted the whole widget and reinstalled a new fresh one. everything seems to be working without the time changing code, but after inserting the new line the widget is not working as it should be.

could this problem be connected with the cache?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding time in php

Post by requinix »

If the changes are saved and it's just the widget that doesn't update, it probably means there was a problem during the AJAX request: the server returned an error, or the data was bad, or something happened in the Javascript. Does your browser show any errors in its console? Do you have PHP set up to log errors anywhere, and if so, are there any errors? Otherwise, what does the browser show as happening during the AJAX request (via its "network"-type logging tag) - meaning does the request return 200, does it return any information, or whatever.
mio510
Forum Newbie
Posts: 12
Joined: Sat Jan 02, 2016 6:37 pm

Re: Adding time in php

Post by mio510 »

i have looked at the browser console and the error that is coming out is

Code: Select all

POST XHR http://localhost/wordpress/wp-admin/admin-ajax.php [HTTP/1.1 200 OK 498ms]
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data jquery.js:5:1599
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Adding time in php

Post by requinix »

Okay. Open up whatever console thing you use (like Chrome's Inspector thing, or Firebug in Firefox) and make sure it's recording network requests (check the Net/Network tab and make sure it doesn't say it's disabled). Then try to change the location. You'll see a request to /wordpress/wp-admin/admin-ajax.php. What is the response it shows for that?
Post Reply