Date and time are setted to Madrid's standard UTC, and stored for translation and formatting in this way:
date_default_timezone_set('Europe/Madrid');
$dia=""; $mes=""; $dia2=""; $ano=""; $horaActual=""; $minutoActual="";
$dia=date("l");
if ($dia=="Monday") {$dia="Lunes";} if ($dia=="Tuesday") {$dia="Martes";} if ($dia=="Wednesday") {$dia="Miércoles";} if ($dia=="Thursday") {$dia="Jueves";} if ($dia=="Friday") {$dia="Viernes";} if ($dia=="Saturday") {$dia="Sabado";} if ($dia=="Sunday") {$dia="Domingo";}
$mes=date("F");
if ($mes=="January") {$mes="Enero";} if ($mes=="February") {$mes="Febrero";} if ($mes=="March") {$mes="Marzo";} if ($mes=="April") {$mes="Abril";} if ($mes=="May") {$mes="Mayo";} if ($mes=="June") {$mes="Junio";} if ($mes=="July") {$mes="Julio";} if ($mes=="August") {$mes="Agosto";} if ($mes=="September") {$mes="Setiembre";} if ($mes=="October") {$mes="Octubre";} if ($mes=="November") {$mes="Noviembre";} if ($mes=="December") {$mes="Diciembre";}
$dia2=date("d");
$ano=date("Y");
$horaActual=date("H");
$minutoActual=date("m");
This gives the same time and date all the time (I created this an hour ago), not refreshing while web browser does. In this right moment, this code:
<?php echo "$dia $dia2 de $mes, $horaActual:$minutoActual"; echo "--" date("F j, Y, g:i a");?>
Shows:
Lunes 26 de Mayo, 16:05 -- May 26, 2014, 5:03 pm
So date() is getting the correct and updated info, but variables are not updating this info, showing stucked data from the first time they stored this values.
every time user gets inside this url, date and time must be updated with actual values
I dont know how your time got stuck, but alternatively you could do this (time updated). Consider this example:
date_default_timezone_set('Europe/Madrid');
$dia = $mes = $dia2 = $ano = $horaActual = $minutoActual = "";
$days = array('Monday' => 'Lunes', 'Tuesday' => 'Martes', 'Wednesday' => 'Miércoles', 'Thursday' => 'Jueves', 'Friday' => 'Viernes', 'Saturday' => 'Sabado', 'Sunday' => 'Domingo');
$months = array('January' => 'Enero', 'February' => 'Febrero', 'March' => 'Marzo', 'April' => 'Abril', 'May' => 'Mayo', 'June' => 'Junio', 'July' => 'Julio', 'August' => 'Agosto', 'September' => 'Setiembre', 'October' => 'Octube', 'November' => 'Noviembre', 'December' => 'Diciembre');
$dia = date("l");
$mes = date("F");
$dia2 = date("d");
$ano = date("Y");
// $horaActual = date("H");
// $minutoActual = date("m");
$time = date('H:i');
echo "$days[$dia] $dia2 de $months[$mes], $time"; echo "--". date("F j, Y, g:i a");
// outputs: Lunes 26 de Mayo, 17:21--May 26, 2014, 5:21 pm
Fiddle
Related
Any help with PHP to convert the below, taking into consideration the time zone at the end, to UNIX epoch for MYSQL insert.
29/05/2022 22:23:04 +00:00
No examples of attempts :-/ Tried basic strtotime on it's own.
Just parse the date and call getTimestamp.
$unixtime = DateTime::createFromFormat('d/m/Y H:i:s P', '29/05/2022 22:23:04 +00:00')->getTimestamp();
echo $unixtime;
Output:
1653862984
In the interest of helping someone out in a pinch .. You need to do something like this. I don't know your exact timezones, if they are +0:00 or +00:00 .. But this will get you started as a stand-alone app that you can form into your own script...
<?php
$zones =array(
'-10:00' => 'America/Atka',
'-09:00' => 'America/Yakutat',
'-08:00' => 'Mexico/BajaNorte',
'-07:00' => 'Mexico/BajaSur',
'-06:00' => 'Mexico/General',
'-05:00' => 'Canada/Eastern',
'-04:-30' => 'America/Caracas',
'-04:00' => 'Chile/Continental',
'-03:-30' => 'Canada/Newfoundland',
'-03:00' => 'Brazil/East',
'-02:00' => 'Brazil/DeNoronha',
'-01:00' => 'Atlantic/Cape_Verde',
'+00:00' => 'Europe/London',
'+01:00' => 'Europe/Zurich',
'+02:00' => 'Europe/Zaporozhye',
'+03:00' => 'Indian/Mayotte',
'+03:30' => 'Asia/Tehran',
'+04:00' => 'Indian/Reunion',
'+04:30' => 'Asia/Kabul',
'+05:00' => 'Indian/Maldives',
'+05:30' => 'Asia/Kolkata',
'+05:45' => 'Asia/Katmandu',
'+06:00' => 'Indian/Chagos',
'+06:30' => 'Indian/Cocos',
'+07:00' => 'Indian/Christmas',
'+08:00' => 'Australia/West',
'+08:45' => 'Australia/Eucla',
'+09:00' => 'Asia/Yakutsk',
'+09:30' => 'Australia/Yancowinna',
'+10:00' => 'Australia/Victoria',
'+10:30' => 'Australia/Lord_Howe',
'+11:00' => 'Asia/Magadan',
'+12:00' => 'Asia/Kamchatka'
);
$my_time = '29/05/2022 22:23:04 +00:00';
$time_split = explode(' ', $my_time);
$my_date = $time_split[0];
$my_time = $time_split[1];
$my_zone = $time_split[2];
$datetime = DateTime::createFromFormat('d/m/Y G:H:s', "$my_date $my_time");
$timezone = new DateTimeZone($zones[$my_zone]);
$datetime->setTimezone($timezone);
echo "Your Date:Time: " . $datetime->format('F d, Y H:i') . "\n";
echo "Your EPOCH: " . strtotime($datetime->format('F d, Y H:i')) . "\n";
I currently am working with a database that has a month column and a year column.. Both are of type text. Month is stored as 'January' and year is stored as you would expect, '2016'..
Any recommendations for concatenating these and converting them to a date type?
You can use a query like this. you only must change the strings to your fieldnames:
sample
select STR_TO_DATE(concat('2016',' ','April','1'), '%Y %M %D');
result
2016-04-01
$months = [
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' =>10,
'november' =>11,
'december' =>12
];
$year = 2016;
$month_text = 'january';
$day = 1;
if($months[strtolower($month_text)]<10){
$month = '0'.$months[strtolower($month_text)];
}else{
$month = $months[strtolower($month_text)];
}
if($day<10){
$day = '0'.$day;
}else{
$day = $day;
}
echo $year.'-'.$month.'-'.$day;
create a new feld in db with type date and insert it into the db. maybe wrap this in a function and put it in a while loop..?
This works for me:
select convert(date,(concat('January',' ','2016')))
I've got bunch of birthdays which are stored in format DDMMMYY. I need to convert those to date values, so i can store those in database.
Is there any easy way of telling strtotime function that date must be in the past?
<?php
$datestring = '22SEP41';
echo date('Y-m-d',strtotime($datestring)); //outputs 2041-09-22, should be 1941-09-22
?>
<?php
$datestring = '22SEP41';
$matches = [];
preg_match('/([0-9]{2})([A-Z]{3})([0-9]{2})/', $datestring, $matches);
$prefix = ($matches[3] <= date('y') ? '20' : '19');
$matches[3] = "{$prefix}{$matches[3]}";
$ts = strtotime("{$matches[1]} {$matches[2]} {$matches[3]}");
// date ('Y-m-d', $ts) == 1941-09-22
This assumes that 22SEP06 should be interpreted as 2006 rather than 1906 - basically it gives the output a range of 1917 -> 2016.
This method create a date of past century only if standard evaluated date is after today:
$date = date_create( $datestring );
if( $date->diff( date_create() )->invert )
{
$date->modify( '-100 years' );
}
echo $date->format( 'Y-m-d' );
For
$datestring = '22SEP41';
the output is:
1941-09-22
For
$datestring = '22SEP01';
the output is:
2001-09-22
eval.in demo
Basically, we create a DateTime based on given string, then we calculate difference with current day; if the difference is negative (->invert), we subtract 1 century from the date.
You can personalize the condition using ->format('%R%Y') instead of ->invert. In this example:
if( $date->diff( date_create() )->format('%R%Y') < 10 )
Dates from 00 through 05 as evaluated as 2000-2005.
You could try something like:
$ds = '22SEP41';
$day = $ds[0].$ds[1];
// Getting the month.
$mon = array(
'JAN' => 1,
'FEB' => 2,
'MAR' => 3,
'APR' => 4,
'MAY' => 5,
'JUN' => 6,
'JUL' => 7,
'AUG' => 8,
'SEP' => 9,
'OCT' => 10,
'NOV' => 11,
'DEC' => 12
);
$mont = $ds[2].$ds[3].$ds[4];
$month = $mon[$mont]; // Gets the month real.
$year = '19'.$ds[5].$ds[6];
$final = $day.'-'.$month.'-'.$year;
I tested it on my local machine and it worked. Hope it works and is what you're looking for :)
The code below would set open and closed on my website. But it is not in the right time zone. How do i set it to the Dutch time zone?
So can some one help me out.
And also i have a question about setting it in days.
So you can set on the website the shippingtime depending on day. So on monday the shipping time is: send today. Or on sunday it is send on monday.
<?php
/**
* Based on the following business hours:
* (Note : I setup the hours for each day if they carry-over)
* everyday is open from 09:00 AM - 12:00 AM
* Sun/Sat open extra from 12:00 AM - 01:00 AM
*/
$storeSchedule = [
'Sun' => ['00:00 AM' => '00:00 AM'],
'Mon' => ['9:00 AM' => '00:00 PM'],
'Tue' => ['9:00 AM' => '05:00 PM'],
'Wed' => ['9:00 AM' => '05:00 PM'],
'Thu' => ['9:00 AM' => '05:00 PM'],
'Fri' => ['9:00 AM' => '05:30 PM'],
'Sat' => ['9:00 AM' => '04:30 PM']
];
// current OR user supplied UNIX timestamp
$timestamp = time();
// default status
$status = 'momenteel gesloten, stuur ons een mail';
// get current time object
$currentTime = (new DateTime())->setTimestamp($timestamp);
// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {
// create time objects from start/end times
$startTime = DateTime::createFromFormat('h:i A', $startTime);
$endTime = DateTime::createFromFormat('h:i A', $endTime);
// check if current time is within a range
if (($startTime < $currentTime) && ($currentTime < $endTime)) {
$status = '<b>bereikbaar op: Tel: 023-5313188</b> of <b>mail</b>';
break;
}
}
echo "Onze klantenservice is $status";
?>
use this for instantiating the current DateTime Object in Dutch Timezone:
$current = new DateTime(
'now',
new DateTimeZone('Europe/Amsterdam')
);
the problem was at the DateTime comparison. Creating a DateTime object from only the time sets the date to 1970-01-01...
/**
* Based on the following business hours:
* (Note : I setup the hours for each day if they carry-over)
* everyday is open from 09:00 AM - 12:00 AM
* Sun/Sat open extra from 12:00 AM - 01:00 AM
*/
$storeSchedule = [
'Sun' => ['00:00 AM' => '00:00 AM'],
'Mon' => ['9:00 AM' => '00:00 PM'],
'Tue' => ['9:00 AM' => '05:00 PM'],
'Wed' => ['9:00 AM' => '05:00 PM'],
'Thu' => ['9:00 AM' => '05:00 PM'],
'Fri' => ['9:00 AM' => '05:30 PM'],
'Sat' => ['9:00 AM' => '04:30 PM']
];
// current OR user supplied UNIX timestamp
$timestamp = '2015-03-12 12:00:00'; // shows it is open
//$timestamp = '2015-03-12 8:59:59'; // shows closed
//$timestamp = '2015-03-12 9:00:00'; // shows open
//$timestamp = 'now'; // shows current status
// default status
$status = 'momenteel gesloten, stuur ons een mail';
// default timezone
$timeZone = new DateTimeZone('Europe/Amsterdam');
// get current time object
$currentTime = new DateTime($timestamp, $timeZone);
// loop through time ranges for current day
foreach ($storeSchedule[date('D', $timestamp)] as $startTime => $endTime) {
// create time objects from start/end times
$startTime = DateTime::createFromFormat(
'Y-m-d h:i A',
$currentTime->format('Y-m-d') . ' ' . $startTime,
$timeZone
);
$endTime = DateTime::createFromFormat(
'Y-m-d h:i A',
$currentTime->format('Y-m-d') . ' ' . $endTime,
$timeZone
);
// check if current time is within a range
if (($startTime <= $currentTime) && ($currentTime <= $endTime)) {
$status = '<b>bereikbaar op: Tel: 023-5313188</b> of <b>mail</b>';
break;
}
}
echo "Onze klantenservice is $status";
How can I display the date in Turkish language?
I'm trying following code but it does not print anything at all.
setlocale(LC_ALL, 'tr_TR.UTF-8');
echo strftime("%e %B %Y %A", time());
Though i don't understand turkish but it is printing output
14 Şubat 2013 Perşembe
So your code is fine.
Hope you are not missing out php tags. :/
<?php
setlocale(LC_ALL, 'tr_TR.UTF-8');
echo strftime("%e %B %Y %A", time());
?>
Prints out: 30 Ekim 2015 Cuma
Code works fine.
Php date for "F" which gives month of English name to translate to Turkish given example
date("d F")
you can use this function
function convertMonthToTurkishCharacter($date){
$aylar = array(
'January' => 'Ocak',
'February' => 'Şubat',
'March' => 'Mart',
'April' => 'Nisan',
'May' => 'Mayıs',
'June' => 'Haziran',
'July' => 'Temmuz',
'August' => 'Ağustos',
'September' => 'Eylül',
'October' => 'Ekim',
'November' => 'Kasım',
'December' => 'Aralık',
'Monday' => 'Pazartesi',
'Tuesday' => 'Salı',
'Wednesday' => 'Çarşamba',
'Thursday' => 'Perşembe',
'Friday' => 'Cuma',
'Saturday' => 'Cumartesi',
'Sunday' => 'Pazar',
'Jan' => 'Oca',
'Feb' => 'Şub',
'Mar' => 'Mar',
'Apr' => 'Nis',
'May' => 'May',
'Jun' => 'Haz',
'Jul' => 'Tem',
'Aug' => 'Ağu',
'Sep' => 'Eyl',
'Oct' => 'Eki',
'Nov' => 'Kas',
'Dec' => 'Ara'
);
return strtr($date, $aylar);
}
add it before the code.
setlocale (LC_ALL, 'tr_TR.UTF-8', 'tr_TR', 'tr', 'turkish');
public function getTurkishDate(){
$locale = 'tr_TR'; // a canonicalized locale
$format = 'dd-MMMM-YYYY'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
$locale, // string locale
IntlDateFormatter::NONE, // int date type
IntlDateFormatter::NONE, // int time type
'UTC', // string timezone
IntlDateFormatter::GREGORIAN, // int cal type
$format // string pattern
);
return $df->format($dt); //string 07-Ağustos-2018
}
OR
public function getTurkishDate(){
$locale = 'tr_TR'; // a canonicalized locale
$format = 'dd-MMMM-YYYY-EEEE'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
$locale, // string locale
IntlDateFormatter::NONE, // int date type
IntlDateFormatter::NONE, // int time type
'UTC', // string timezone
IntlDateFormatter::GREGORIAN, // int cal type
$format // string pattern);
return $df->format($dt); //string 07-Ağustos-2018-Salı
}
OR
public function getTurkishDate(){
$locale = 'tr_TR'; // a canonicalized locale
$format = 'dd-MMMM-YYYY-ee'; // ISO format codes, not the typical date ones
$dt = new DateTime(); // a DateTime object
$df = new IntlDateFormatter(
$locale, // string locale
IntlDateFormatter::NONE, // int date type
IntlDateFormatter::NONE, // int time type
'UTC', // string timezone
IntlDateFormatter::GREGORIAN, // int cal type
$format // string pattern);
return $df->format($dt); //string 07-Ağustos-2018-02
}
You can take current date and time with:
<?php
date_default_timezone_set("Europe/Istanbul");
//echo "The time is " . date("H:i") . "<br>";
//echo "Today is " . date("Y-m-d") . "<br>";
//echo "Today is " . date("l");
$now_time = date("H:i");
$now_date = date("Y-m-d");
?>