Date and time in Greek - php

I'm currently using a website to get the time in Athens:
$d = new DateTime("now", new DateTimeZone("Europe/Athens"));
echo $d->format("l, d M Y");
But I would like the date to be displayed in Greek and in the same format.

The full answer:
date_default_timezone_set('Europe/Athens');
setlocale(LC_TIME, 'el_GR.UTF-8');
echo strftime('%A ');
$greekMonths = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
$greekDate = date('j') . ' ' . $greekMonths[intval(date('m'))-1] . ' ' . date('Y');
echo $greekDate;
this will display the date like:
Πέμπτη 4 Φεβρουαρίου 2013
UPDATE: For the above chunk to work it is very important to set your PHP locale to Greek.
This is an alternative:
date_default_timezone_set('Europe/Athens');
setlocale(LC_TIME, 'el_GR.UTF-8');
$day = date("w");
$greekDays = array( "Κυριακή", "Δευτέρα", "Τρίτη", "Τετάρτη", "Πέμπτη", "Παρασκευή", "Σάββατο" );
$greekMonths = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
$greekDate = $greekDays[$day] . ' ' . date('j') . ' ' . $greekMonths[intval(date('m'))-1] . ' ' . date('Y');
echo $greekDate;

I was in the quest to find the same thing but I didn't find a full solution as I needed it. In my case I need to write the posted datetime of the article in Greek like Αναρτήθηκε Σάββατο 2 Μαΐου 2015.
So using some code of costastg answer, I managed to put together the following function.
I am sure there are other solutions out there:
function formatToGreekDate($date){
//Expected date format yyyy-mm-dd hh:MM:ss
$greekMonths = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
$greekdays = array('Δευτέρα','Τρίτη','Τετάρτη','Πέμπτη','Παρασκευή','Σάββατο','Κυριακή');
$time = strtotime($date);
$newformat = date('Y-m-d',$time);
return $greekdays[date('N', strtotime($newformat))-1].' '. date('j', strtotime($newformat)).' '.$greekMonths[date('m', strtotime($newformat))-1]. ' '. date('Y', strtotime($newformat)); // . ' '. $date;
}

You should use setlocale and strftime, an example to get the idea:
setlocale(LC_TIME, 'el_GR.UTF-8');
// Your display formatting from https://www.php.net/manual/en/function.strftime.php
$field_date_format = '%A, %d %B, %Y';
// Will display= Greek: Πέμπτη, 14 Φεβρουάριος, 2013
echo "Greek: ".strftime($field_date_format, strtotime("14-02-2013"));
I know "Φεβρουάριος" is not "Φεβρουάριου" but if is not crucial to your needs you can use this.

If I'm right you want to display the l parameter in Greek. You should set your PHP locale to Greek then.
Use setlocale(): http://php.net/manual/en/function.setlocale.php
setlocale(LC_TIME, 'greek');

You should have everything you need using date:
$greekMonths = [
'Ianouarios', 'Fevrouarios', 'Martios', 'Aprilios', 'Maios',
'Iounios', 'Ioulios', 'Avgoustos', 'Septemvrios', 'Oktovrios',
'Noemvrios', 'Dekemvrios'
];
$greekDate = date('d') . ' ' . $greekMonths[intval(date('m'))-1] . ' ' . date('Y');
echo $greekDate;
Second way using setlocale:
setlocale(LC_CTYPE, 'greek');
setlocale(LC_TIME, 'greek');

A simple method that works for all formats
function date_greek($format,$time=null){
if($time===null)$time=time();
$date = date($format,$time);
if(strpos($format,'F')!==false){
$en_months = array('January','February','March','April','May','June','July','August','September','October','November','December');
$el_months = array('Ιανουαρίου','Φεβρουαρίου','Μαρτίου','Απριλίου','Μαΐου','Ιουνίου','Ιουλίου','Αυγούστου','Σεπτεμβρίου','Οκτωβρίου','Νοεμβρίου','Δεκεμβρίου');
}else{
$en_months = array('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$el_months = array('Ιαν','Φεβ','Μαρ','Απρ','Μαι','Ιουν','Ιουλ','Αυγ','Σεπ','Οκτ','Νοε','Δεκ');
}
$en_days = array('Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday');
$el_days = array('Δευτέρα','Τρίτη','Τετάρτη','Πέμπτη','Παρασκευή','Σάββατο','Κυριακή');
$en_days_s = array('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
$el_days_s = array('Δευ','Τρι','Τετ','Πεμ','Παρ','Σαβ','Κυρ');
$en_ampm = array('AM','PM');
$el_ampm = array('ΠΜ','ΜΜ');
$date_el = str_replace($en_months,$el_months,$date);
$date_el = str_replace($en_days,$el_days,$date_el);
$date_el = str_replace($en_days_s,$el_days_s,$date_el);
$date_el = str_replace($en_ampm,$el_ampm,$date_el);
return $date_el;
}
echo date_greek('l, d M Y',strtotime('2021-04-25')); // Πέμπτη, 22 Απρ 2021
echo date_greek('l j F Y h:i:s A',strtotime('2021-04-25')); // Πέμπτη 22 Απριλίου 2021 12:00:00 ΠΜ

Related

how to get 4-days ago to now - php

If current date is 2016-03-06, I would like to get these dates :
2016-03-06
2016-03-05
2016-03-04
2016-03-03
I'm trying to get this purpose but my result not what I want :
$_4date = date("y-m-d",strtotime("day"));
$_3date = date("y-m-d",strtotime("-1 day"));
$_2date = date("y-m-d",strtotime("-2 day"));
$_1date = date("y-m-d",strtotime("-3 day"));
echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;
the result is :
70-01-01
16-03-05
16-03-04
16-03-03
To get today's date with strtotime, you do strtotime("today");. However, as Bjorn has commented, you can simply just call date() directly.
Furthermore, the reason you are not getting the year in four digits is because you are using a lowercase y instead of an uppercase Y.
Try date("Y-m-d", strtotime("-1 day"));.
The following piece of code illustrates the required changes:
$today = date("Y-m-d");
$yesterday = date("Y-m-d", strtotime("-1 day"));
echo "$today <br />";
echo "$yesterday <br />";
// Output
2016-03-06
2016-03-05
For more informtation, please consult the PHP documentation on the date function. It actually shows you that what to expect from y and Y and it also shows you that the default value that is passed as the second argument is time(), meaning the default is the current time.
PHP's strtotime documentation can be consulted for more information on the strtotime() function and its possible parameters.
Always check the (PHP) documentation first before asking a question.
You need to use like that:
$_4date = date("Y-m-d");
$_3date = date("Y-m-d",strtotime("-1 day"));
$_2date = date("Y-m-d",strtotime("-2 day"));
$_1date = date("Y-m-d",strtotime("-3 day"));
Explanation:
For current date no need to use use strtotime().
For full year you need to use this format Y-m-d.
y-m-d will return you the date 16-03-06 but Y-m-d will return you 2016-03-06.
Use a for loop with strtotime( "... days ago" ):
for( $i = 0; $i < 4; $i++ )
{
echo date( 'Y-m-d', strtotime( "$i days ago" ) ) . PHP_EOL;
}
The first loop (0 days ago) will output today date, other loops will output past days.
3v4l.org demo
You need to use capital 'Y' for full year (2016) instead of small 'y' which will display year in shorthand (16).
And for current date just use date("Y-m-d").
<?php
$_4date = date("Y-m-d");
$_3date = date("Y-m-d",strtotime("-1 day"));
$_2date = date("Y-m-d",strtotime("-2 day"));
$_1date = date("Y-m-d",strtotime("-3 day"));
echo $_4date;
echo '<br />';
echo $_3date;
echo '<br />';
echo $_2date;
echo '<br />';
echo $_1date;
?>
Working Example
<?php
$date = [date("Y-m-d")];
for($i = 1; $i < 4; $i++) {
$date[] = date("Y-m-d",strtotime("-$i day"));
}
//For cli output you'll need:
echo implode("\n", $date) . "\n";
//For web output you'll need:
echo implode("<br />", $date) . "<br />";

Setting strtotime adding a variable to amount of days

I am trying to export a certain number of days and setting it to the variable $max_future. Currently it is a set amount of days but I want the users input variable to be the number it has instead.
Currently it is set as:
$max_future = date("Y-m-d", strtotime($today . "+6 days"));
I want something like:
$exportDays = '9'; //(or whatever the user input was)
$max_future = date("Y-m-d", strtotime($today . "+$exportDays days"));
Is this possible? I appreciate any help
Try:
$max_future = date("Y-m-d", strtotime($today . "+" . $exportDays . " days"));
Something like this :
<?php
$output = 9; // you can take this value from input.
$today = date("Y-m-d");
echo date('Y-m-d', strtotime($today. ' +'.$output. ' days'));
?>
<?php
$exportDays = $_REQUEST['exportDays']; // or however you want to get it from user input
$max_date = date('Y-m-d', strtotime(date('Y-m-d') . ' +' . $exportDays . ' days'));

Timestamp wrong time

This is my problem
My default timezone is date_default_timezone_set('Europe/Madrid')
actual time 2012-12-06 17:00:38
start time 2012-12-10 16:30:00
unix timestamp actual time 1354809638
unix timestamp start time 1350052200
Start time is greater then actual time. But "unix timestamp actual time" is greater then "unix timestamp start time".
I wonder why?
RELEVANT CODE:
$to_unix_startTime = new DateTime($postField['startTime']);
$UnixStartTime = $to_unix_startTime->format('U');
$date = new DateTime();
$actualTime = $date->getTimestamp();
$start = new DateTime($postFields['startAuct']);
$start->format('Y-m-d H:i:s').PHP_EOL;
$start_tmstmp = date_timestamp_get($start);
$date = new DateTime();
$timestamp = $date->getTimestamp();
if ($timestamp > $start_tmstmp) {
echo $auctStatus = 1;
} elseif ($timestamp < $start) {
echo $auctStatus = 4;
}
$postField['startTime'] is in the wrong format. How do I know this? The timestamp you got from the start date, 1350052200, represents date 2012-10-12 (October 12th 2012) in stead of 2012-12-10 (December 10th 2012).
$postField['startTime'] is probably in the format 10/12/2012 (which will be parsed by DateTime as an American style date), when it should be formatted as 10-12-2012 (which will be parsed by DateTime as a European style date).
So, concluding, check the format of $postField['startTime'] and make sure it is a European style representation of a date.
[edit] Compare:
$european = new DateTime( '10-12-2012' );
$american = new DateTime( '10/12/2012' );
echo $european->format( 'Y-m-d' ) . ' (' . $european->getTimestamp() . ')' . PHP_EOL;
echo $american->format( 'Y-m-d' ) . ' (' . $american->getTimestamp() . ')' . PHP_EOL;
There is no need to format the datetime before getting the timestamp.
Check my code here: https://compilr.com/shadyyx/datetime/index.php
Should there be any problem with the link, here is the code without output:
<?php
date_default_timezone_set('Europe/Madrid');
$start = new DateTime('2012-12-10 16:30:00');
$actual = new DateTime('2012-12-06 17:00:38');
echo $start->format('d.m.Y H:i:s').PHP_EOL;
echo $actual->format('d.m.Y H:i:s').PHP_EOL;
echo PHP_EOL;
$start_tmstmp = $start->getTimestamp();
$act_tmstmp = $actual->getTimestamp();
echo $start_tmstmp.PHP_EOL;
echo $act_tmstmp.PHP_EOL;
echo PHP_EOL;
var_dump($start_tmstmp > $act_tmstmp);

Timestamp YYYY-MM-DDTHH:MM:SS+00:00

How do I get the "UTC, PST, EST, etc.." name from this time offset?
You can copy/paste this in any PHP file and it will run.
<?php
date_default_timezone_set('UTC');
$time = '2012-01-01T21:15:00+00:00';
echo '<pre>';
echo "<b>Step 1 (Have the date now)</b>\n";
$time = explode('T', $time);
print_r($time);
echo "\n\n<b>Step 2 (Get the offset)</b>\n";
$offset = explode('+', $time[1]);
$time[1] = $offset[0];
print_r($offset);
echo "\n\n";
echo date('l F jS', strtotime($time[0])) . ', at ' . date('g:ia', strtotime($time[1]));
echo "\n\n";
//
// How do I matchup the offset in this list?
// The values are -18000, -7200
//
$b = timezone_abbreviations_list();
print_r($b);
Note: I've also tried using echo date('c', $time); but perhaps I have something wrong its giving me an error.
In PHP 5.1.0+ echo date('e', $time); will return the timezone information.

strtotime question

This should be a simple fix;
I currently have a calendar for selecting a range of date:
http://protekco.com/index.php/en/reservations/1718-carvelle-drive-westpalm-beach.html
When the dates are selected in the calendar, it populates 2 input fields for Check in and Check out dates. The problem is, the calendar initially was set 1 day late, so Thursday Sept 22 actually showed as a Thursday Sept 21. I was able to just echo a +1 on the day count, but the input boxes still show the erroneous date. Essentially, I'm trying to add the same +1, but because the date is returned as yyyy/mm/dd, +1 doesn't do much.
Here is the code:
$listsFrom = $this->lists['from'];
$selectedFrom = $listsFrom ? strtotime($listsFrom) : 0;
$listsTo = $this->lists['to'];
$selectedTo = $listsTo ? strtotime($listsTo) : $selectedFrom;
if ($selectedTo) {
ADocument::addDomreadyEvent('Calendars.checkOut = ' . $selectedTo . ';');
}
if ($selectedFrom) {
ADocument::addDomreadyEvent('Calendars.checkIn = ' . $selectedFrom . ';');
}
$listOperation = $this->lists['operation'];
ADocument::addDomreadyEvent('Calendars.operation = ' . $listOperation . ';');
Any ideas?
Thank you!
Edit:
<td class="<?php echo implode(' ', $class); ?>" <?php if ($canReserveBox) { ?> id="day<?php echo $firstDay->Uts+84600000; ?>" onclick="Calendars.setCheckDate('day<?php echo $firstDay->Uts+84600000; ?>','<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_MYSQL_DATE, 0); ?>','<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_NORMAL); ?>')"<?php } ?> >
<span class="date" >
<?php echo AHtml::getDateFormat($firstDay->date, ADATE_FORMAT_NICE_SHORT)+1;
?>
</span>
This is what actually calls the changes. The + 84600000 is my addition, it doesn't seem to do much tho.
If the date is in yyyy/mm/dd format, first use strtotime() to convert it to a timestamp, and then you can use it again to add one day.
$date = '2011/06/01';
$ts = strtotime('+24 hours', strtotime($date));
$date = date('Y/m/d', $ts);
That is just one possible solution.

Categories