CodeIgniter Forums
how to get current year - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: how to get current year (/showthread.php?tid=43549)



how to get current year - El Forum - 07-15-2011

[eluser]mehwish[/eluser]
hi can anyone tell me how to get current year month and date separately .. i have gone through the date helper in CI but year month etc. cannot be separated I want them separate.


how to get current year - El Forum - 07-15-2011

[eluser]Mirge[/eluser]
Code:
$year = date("Y"); // 2011
$shortYear = date("y"); // 11

$numericMonth = date("m"); // 01 through 12
$numericMonth2 = date("n"); // 1 through 12 (no leading zero)
$fullMonth = date("F"); // January through December
$shortMonth = date("M"); // Jan through Dec

See: http://us2.php.net/manual/en/function.date.php


how to get current year - El Forum - 07-15-2011

[eluser]mehwish[/eluser]
I cannot use this function becuase it may take year from client's (user's) pc which cannot be guaranteed to be right. thanks for the help but if there is any other method to get current year please do tell me


how to get current year - El Forum - 07-15-2011

[eluser]Mirge[/eluser]
[quote author="mehwish" date="1310762338"]I cannot use this function becuase it may take year from client's (user's) pc which cannot be guaranteed to be right. thanks for the help but if there is any other method to get current year please do tell me[/quote]

PHP is server-side, not client-side (ie: javascript).

The code above that I gave you will pull the current year from the server, not client.

BTW, CodeIgniter is written with PHP in case you were unaware.


how to get current year - El Forum - 07-15-2011

[eluser]mehwish[/eluser]
do JQuery has any api for getting the current year?


how to get current year - El Forum - 07-15-2011

[eluser]Mirge[/eluser]
[quote author="mehwish" date="1310763047"]do JQuery has any api for getting the current year?[/quote]

jQuery = javascript = client-side. Javascript, of course, has a way to get the client's date... but you just said you didn't want the client's date.


how to get current year - El Forum - 07-15-2011

[eluser]mehwish[/eluser]
no no nothing is an issue if you know any method in jquery please tell me


how to get current year - El Forum - 07-15-2011

[eluser]Mirge[/eluser]
[quote author="mehwish" date="1310767880"]no no nothing is an issue if you know any method in jquery please tell me[/quote]

Go here: http://tinyurl.com/63epvec


how to get current year - El Forum - 08-04-2011

[eluser]John Murowaniecki[/eluser]
You can use jQuery to make a JSON request to a controller such as CodeIgniter below:
Code:
<?php
class Ajax extends CI_Controller
{
    function year()
    {
        print(json_encode(array(
            'long' => date('Y'),
            'short' => date('y')
        )));
    }
    #

}
#
/* eof: controller/ajax.php */
So if you request the url http://yourdomain/yourapplicationfolder/ajax/year you will receive a JSON formated string: {"long":"2011","short":"11"}. So if you need an how-to get data using jQuery.Ajax hereĀ“s a tip:
Code:
// <![CDATA[
    $.ajax({
        url: 'http://yourdomain/yourapplicationfolder/ajax/year',
        type: 'GET',
        dataType: 'json',
        success: function(data) {
            alert('long '+data.long+'\nshort '+data.short);
        }
    });
// ]]>

More information about jQuery and Ajax you'll find here http://api.jquery.com/jQuery.ajax/ , if you need to know how json works I recomend you to read http://www.json.org/ .