How to show numerical date in mm/dd/yy format - php

Right now i am the month, day, and year of a user seperated in three db fields titled month, day, year.
When i display it i am doing:
$month = $row['month'];
$day = $row['day'];
$year = $row['year'];
then to echo it:
$month/$day/$year
The problem is that the PHP is doing mathematics here and dividing the numbers... What can i do to not make that happen and let it simply display the dates..
Thanks

try this out :
echo "{$month}/{$day}/{$year}";

echo $month.'/'.$day.'/'.$year;

date('m/d/Y',strtotime($month . ' ' . $day . ' ' . $year));
The advantage of this is that you can choose how to format the date independent of how it is stored in your database: http://php.net/manual/en/function.date.php

echo $month . "/" . $day . "/" . $year;
Doing string concatenation.
or
echo "{$month}/{$day}/{$year}";
Doing string interpolation.
See the difference/performance of the two here.

Related

Reset automatic code number every year on laravel

guys! i have automatic code number for invoice. the automatic code number is "xx/month/year". i want "xx" in my automatic code number reset to 1 when the year change or every 1 january. this is my code
function noinvoice()
{
$latest = Pengiriman::latest()->first();
$month = date('m');
$year = date('Y');
if (!$latest) {
return '1/' . $month . '/' . $year;
}
$string = preg_replace("/[^0-9\.]/", '', $latest->noinvoice);
return sprintf($string + 1) . '/' . $month . '/' . $year;
}
You need to inspect your $latest object. If $latest doesn't exist OR its year doesn't match this year, then the invoice number is 1. Otherwise, it's $latest->noinvoice + 1.
You never mentioned where you store your invoice creation date, so I'll assume you use the default created_at Laravel approach. For this task you should add one more condition to your if statement:
if (!$latest or date('Y', strtotime($latest->created_at)) != $year) {

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'));

Take two variables for Date and Time and combine to make one date

I would like to take two variables, one representing the date, and another for the time, and then combine them to make one date.
Then I would like to use that combined date and time to check to see if the current date and time is 24 hours or less away from the combined date and time.
$game_date = $game['date'];
$game_time = $game['time'];
$combined_date_and_time = date('$game_date $game_time');
$game_date reads is stored as so: 03/28/2013
$game_time reads is stored as so: 07:05 pm
On output, $combined_date_and_time reads as:
$2pm03America/Los_Angeles_28pm31America/Los_Angeles $2pm03America/Los_Angeles_313803America/Los_Angeles
which of course isn't right.
Is it possible to combine the two into a single date and then compare?
$game_date = game['date'];
$game_time = game['time'];
$combined_date_and_time = $game_date . ' ' . $game_time;
$past_date = strtotime($combined_date_and_time);
$hours = 24;
// Check if date is more recent than the specified number of hours
if ((time() - $past_date) < (60 * 60 * $hours))
{
echo 'Yes!';
}
But, be careful! If your date is ever stored in the European format (dd/mm/yyyy) you'll need to change the order before parsing the date, otherwise it won't work.
If the date separator is - or ., then the European format is assumed.
if (strpos($game_date, '-') !== false)
{
list($day, $month, $year) = explode('-', $game_date);
$game_date = $month . '/' . $day . '/' . $year;
}
else if (strpos($game_date, '.') !== false)
{
list($day, $month, $year) = explode('.', $game_date);
$game_date = $month . '/' . $day . '/' . $year;
}
The above code would go in between the initialization of the date variables and the combined_date_and_time variable.
Use strtotime instead of date:
php > $dt = strtotime("03/28/2013 07:05 pm");
php > echo date("Y-m-d H:i:s", $dt);
2013-03-28 19:05:00
(Also, variables are not expanded in single-quoted ' strings, use double quoted " strings instead)

Date and time in Greek

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 ΠΜ

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