I've a Date-Ouput and format-string in my Query:
$date = $sql->getValue("date");
$datenew = DateTime::createFromFormat('Y-m-d', $date);
$dateex = $datenew->format('l, d.m.Y');
// Output: Monday, 19.12.2016
Is there a way to get the "Monday" in german ("Montag")?
Yes. You can use setlocale() :
setlocale(LC_TIME, 'de_DE', 'deu_deu');
$date = date('l, d.m.Y');
echo $date; //outputs: Montag, 19.12.2016
Have a look at: http://php.net/manual/en/function.setlocale.php
Or you could use a workaround like:
$date = date('l, d.m.Y');
$arrDate = explode(",",$date);
$weekDay = "";
switch($arrDate[0])
{
case 'Monday': $weekDay = 'Montag, ';
break;
case 'Tuesday': $weekDay = 'Dienstag, ';
break;
case 'Wednesday': $weekDay = 'Mittwoch, ';
break;
.
.
.
case 'Sunday' : $weekDay = 'Sonntag, ';
break;
}
echo $weekDay.$arrDate[1];
Related
I want to creat a table for listing 3 future days with day name and date for my online shopping.
I try this code but it doesn't work correctly :
function week_from_monday($date) {
// Assuming $date is in format DD-MM-YYYY
list($day, $month, $year) = explode("-", $_REQUEST["date"]);
// Get the weekday of the given date
$wkday = date('l',mktime('0','0','0', $month, $day, $year));
switch($wkday) {
case 'Monday': $numDaysToMon = 0; break;
case 'Tuesday': $numDaysToMon = 1; break;
case 'Wednesday': $numDaysToMon = 2; break;
case 'Thursday': $numDaysToMon = 3; break;
case 'Friday': $numDaysToMon = 4; break;
case 'Saturday': $numDaysToMon = 5; break;
case 'Sunday': $numDaysToMon = 6; break;
}
// Timestamp of the monday for that week
$monday = mktime('0','0','0', $month, $day-$numDaysToMon, $year);
$seconds_in_a_day = 86400;
// Get date for 7 days from Monday (inclusive)
for($i=0; $i<7; $i++)
{
$dates[$i] = date('Y-m-d',$monday+($seconds_in_a_day*$i));
}
return $dates;
}
$ddate = date('Y-m-d');
$date = new DateTime($ddate);
$week = $date->format("W");
$week_number = $week;
$year = date('Y');
for($day=5; $day<=30; $day++)
{
echo date('m/d/Y', strtotime($year."W".$week_number.$day))."\n";
}
Here is my code but I want to get this :
today : Sun
Monday 30
Tuesday 31
Wednesday 1
To get the next 3 upcoming dates, you can simplify this a lot using date and strotime.
Try this code:
function nextThreeDays($date) {
$dateTs = strtotime($date);
// Dont need this, but I leave it for educational purpose:
// $day_of_week = date("N", $dateTs) - 1; // Mon=0 Tue=1 Wed=2 ...
// $monday_time = strtotime(date("d.m.Y H:i:s", $dateTs) . " -".$day_of_week." days"); // timestamp of monday of week of $date
for($i = 1; $i <= 3; $i++) {
echo date('m/d/Y', strtotime(date("d.m.Y H:i:s", $dateTs) . " +".$i." days"))."\n";
}
}
// Put in "today : Sun"
nextThreeDays("29.01.2023");
Outputs:
// Monday 30 Tuesday 31 Wednesday 1
01/30/2023
01/31/2023
02/01/2023
Need to add a specified numbers of days to a selected date. Not the current date. Im using strtotime in this fashion:
$date = $_POST['r_date'];
$r_date = $date;
$txts_rental = $_POST['txts_rental'];
switch ($txts_rental){
case "250.00":
$s_length = "30";
break;
case "575.00":
$s_length = "90";
break;
case "975.00":
$s_length = "180";
break;
case "1200.00":
$s_length = "365";
break;
}
$rn_date = date( "Y-m-d", strtotime( "$s_length day" ) );
the date( "Y-m-d" needs to be the the $r_date variable in order to get the renewal date from the inputted date
You can use
$EndDateTime = DateTime::createFromFormat('d/m/Y', "16/07/2017");
$EndDateTime->modify('+6 days');
echo $EndDateTime->format('d/m/Y');
or
$today = "2015-06-15"; // Or can put $today = date ("Y-m-d");
$fiveDays = date ("Y-m-d", strtotime ($today ."+5 days"));
You need to change your method as below. add the target date in second parameter so that the date not added in current date
$date = $_POST['r_date'];
$r_date = $date;
$txts_rental = $_POST['txts_rental'];
switch ($txts_rental){
case "250.00":
$s_length = "30";
break;
case "575.00":
$s_length = "90";
break;
case "975.00":
$s_length = "180";
break;
case "1200.00":
$s_length = "365";
break;
}
$rn_date = date( "Y-m-d", strtotime( "$date +".$s_length." day" ) );
Im selecting a date in my input date with jQuery datepicker.
The format that Im using in datepicker script is: dateFormat: 'DD, d MM, yy',
So I get this date in my input: "Quinta-feira, 1 Maio, 2014" (portuguese date).
But now I need to convert this date to save like datetime in mysql.
I needed only to use this code below if the date was in english:
$date = DateTime::createFromFormat('l, j F, Y', $_POST['date']);
echo $date->format('Y-m-d');
But my date isnt english so I need to do the conversion and Im trying to use the function below" convertDate" to do this.
But when I call the function, and I pass the input date value like this: convertDate($_POST['date']);
I got an error "Call to a member function format() on a non-object in this line: $day= $date->format("l");
Do you see something wrong here? Because the fucntion seems good for me!
function convertDate($myDate){
$date = DateTime::createFromFormat('Ymd', $myDate);
$day = $date->format("l");
$daynum = $date->format("j");
$month = $date->format("F");
$year = $date->format("Y");
switch($day)
{
case "Segunda-Feira": $day = "Monday"; break;
case "Terça-Feira": $day = "Tuesday"; break;
case "Quarta-Feira": $day = "Wednesday"; break;
case "Quinta-Feira": $day = "Thursday"; break;
case "Sexta-Feira": $day = "Friday"; break;
case "Sábado": $day = "Saturday"; break;
case "Domingo": $day = "Sunday"; break;
default: $day = "Unknown"; break;
}
switch($month)
{
case "Janeiro": $month = "January"; break;
case "Fevereiro": $month = "February"; break;
case "Março": $month = "March"; break;
case "Abril": $month = "April"; break;
case "Maio": $month = "May"; break;
case "Junho": $month = "June"; break;
case "Julho": $month = "July"; break;
case "Agosto": $month = "August"; break;
case "Setembro": $month = "September"; break;
case "Outubro": $month = "October"; break;
case "Novembro": $month = "November"; break;
case "Dezembro": $month = "December"; break;
default: $month = "Unknown"; break;
}
echo $daynum . ", " . $month . ", " . $year;
}
Trying with str_ireplace:
$english = array("Segunda-Feira","Terça-Feira","Quarta-Feira","Quinta-Feira","Sexta-Feira","Sábado","Domingo");
$portuguese = array("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday");
$result= str_ireplace ($english , $portuguese, $_POST['date']);
$date = DateTime::createFromFormat('l, j F, Y', $result);
echo $date->format('Y-m-d');
I got same error in echo $date->format('Y-m-d')
Instead of trying to translate dates in words in possibly various languages, you can have JQuery drop a UTC of sorts in a hidden form field and work with this instead:
This will return a JS date object:
var currentDate = $( ".selector" ).datepicker( "getDate" );
This will make a PHP UTC from it (it's microseconds in JS...)
var phputc = Math.ceil((currentDate.getTime()/1000));
This will put in in a hidden form:
$(".selector" ).change(
function()
{
var currentDate = $( ".selector" ).datepicker( "getDate" );
var phputc = Math.ceil((currentDate.getTime()/1000));
document.forms[0].nameofhiddenformfield.value = phputc;
}
);
Make sure there is a field like this in your form
<input type="hidden" name="nameofhiddenformfield" />
And on the PHP side you can then do:
$dateUTC = $_POST['nameofhiddenformfield'];
echo date('Y-m-d',$dateUTC);
Your approach is wrong, in the first line of your function you try to convert your Portuguese date as if it is in the Ymd format so your $date object is flawed to begin with.
What you should probably do (assuming that there is no localized function to convert Portuguese dates to DateTime...), is use something like a string replace to replace all Portuguese words with English words and after that use your original conversion:
$date = DateTime::createFromFormat('l, j F, Y', $translated_date);
The 17 / 34 words concerned don't interfere with each other so str_ireplace() with two arrays should do it.
How to get year and month from a given date.
e.g. $dateValue = '2012-01-05';
From this date I need to get year as 2012 and month as January.
Use strtotime():
$time=strtotime($dateValue);
$month=date("F",$time);
$year=date("Y",$time);
Using date() and strtotime() from the docs.
$date = "2012-01-05";
$year = date('Y', strtotime($date));
$month = date('F', strtotime($date));
echo $month
Probably not the most efficient code, but here it goes:
$dateElements = explode('-', $dateValue);
$year = $dateElements[0];
echo $year; //2012
switch ($dateElements[1]) {
case '01' : $mo = "January";
break;
case '02' : $mo = "February";
break;
case '03' : $mo = "March";
break;
.
.
.
case '12' : $mo = "December";
break;
}
echo $mo; //January
I'm using these function to get year, month, day from the date
you should put them in a class
public function getYear($pdate) {
$date = DateTime::createFromFormat("Y-m-d", $pdate);
return $date->format("Y");
}
public function getMonth($pdate) {
$date = DateTime::createFromFormat("Y-m-d", $pdate);
return $date->format("m");
}
public function getDay($pdate) {
$date = DateTime::createFromFormat("Y-m-d", $pdate);
return $date->format("d");
}
You can use this code:
$dateValue = strtotime('2012-06-05');
$year = date('Y',$dateValue);
$monthName = date('F',$dateValue);
$monthNo = date('m',$dateValue);
printf("m=[%s], m=[%d], y=[%s]\n", $monthName, $monthNo, $year);
I will share my code:
In your given example date:
$dateValue = '2012-01-05';
It will go like this:
dateName($dateValue);
function dateName($date) {
$result = "";
$convert_date = strtotime($date);
$month = date('F',$convert_date);
$year = date('Y',$convert_date);
$name_day = date('l',$convert_date);
$day = date('j',$convert_date);
$result = $month . " " . $day . ", " . $year . " - " . $name_day;
return $result;
}
and will return a value: January 5, 2012 - Thursday
$dateValue = '2012-01-05';
$yeararray = explode("-", $dateValue);
echo "Year : ". $yeararray[0];
echo "Month : ". date( 'F', mktime(0, 0, 0, $yeararray[1]));
Usiong explode() this can be done.
$dateValue = '2012-01-05';
$year = date('Y',strtotime($dateValue));
$month = date('F',strtotime($dateValue));
I personally prefer using this shortcut. The output will still be the same, but you don't need to store the month and year in separate variables
$dateValue = '2012-01-05';
$formattedValue = date("F Y", strtotime($dateValue));
echo $formattedValue; //Output should be January 2012
A little side note on using this trick, you can use comma's to separate the month and year like so:
$formattedValue = date("F, Y", strtotime($dateValue));
echo $formattedValue //Output should be January, 2012
$dateValue = strtotime($q);
$yr = date("Y", $dateValue) ." ";
$mon = date("m", $dateValue)." ";
$date = date("d", $dateValue);
I am digging in the code this professional programmer had put together for an events website for me and I found this function that seems super confusing, as it looks like 1 line of code with "strtotime" would do the same thing.
Is it because we wanted to use "Jan 2nd" format with the two letters after the day of the month?
function convert_date_to_web($date) {
if (empty($date) || $date == "0000-00-00") return $date;
switch ($date[5].$date[6]) {
case '01':
$month = 'Jan';
break;
case '02':
$month = 'Feb';
break;
case '03':
$month = 'Mar';
break;
case '04':
$month = 'Apr';
break;
case '05':
$month = 'May';
break;
case '06':
$month = 'Jun';
break;
case '07':
$month = 'Jul';
break;
case '08':
$month = 'Aug';
break;
case '09':
$month = 'Sep';
break;
case '10':
$month = 'Oct';
break;
case '11':
$month = 'Nov';
break;
case '12':
$month = 'Dec';
}
if($date[9] == "1") {
$day = $date[9]."st";
}
else if ($date[9] == "2") {
$day = $date[9]."nd";
}
else if ($date[9] == "3") {
$day = $date[9]."rd";
}
else {
$day = $date[9]."th";
}
if($date[8] != "0") {
$day = $date[8].$day;
}
$date = $month." ".$day.", ".$date[0].$date[1].$date[2].$date[3];
return $date;
}
And my version is
$timestamp = strtotime($event->start_date);
$start_date = date("M d, Y", $timestamp);
Here is an exact replicate of the original function. I've used two versions, the first uses strtotime, the second uses explode to avoid using strtotime.
$date = '2011-10-01';
$date_format = 'M jS, Y'; // Three letter month, day name w/o leading zero, day suffix, year
echo date( $date_format, strtotime( $date)) . "\n";
$date_pieces = explode( '-', $date);
echo date( $date_format, mktime( 0, 0, 0, $date_pieces[1], $date_pieces[2], $date_pieces[0]));
Demo
Yup. I think that is what the reason for that long function is. It is to add in the "st", "nd", "rd", and the "th".
Your code does not do that. Maybe you can try it this way? (Needs debugging, cos i never tested it on actual php)
$timestamp = strtotime($event->start_date);
$start_date = date("M d", $timestamp);
$lastNum = substr($start_date, -1)
switch($lastNum) {
case "0":
break;
case "1":
$start_date .= "st"; break;
case "2":
$start_date .= "nd"; break;
case "3":
$start_date .= "rd"; break;
default:
$start_date .= "th"; break;
}
$start_date .= date(", Y", $timestamp);
The reason why I prefer such explode()ish solutions over strtotime(), too, is that they give deterministic, portable results that are less surprising while strtotime() does some complex magic.
As with all programming there is more than one way to do something. Experience teaches programmer to do things a certain way. From the look of this this function if for converting the default output format of mysql dates into a version that converts the month to a string. It looks good to me