PHP Convert date Timezone to Epoch - php

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";

Related

Get Start and end date of Week according to string

I want to display the start date and end date of the week. I have One date and a string like 1W4 and,in 1W4 consider 4 weeks and 1 visit so, my string like this 2W4,1W2,3W3,1W1,2W4.
I want to make start date and end date of week array according to string and week start from Sunday to Saturday.
Please post me if anyone has solution.Please ignoring if mistake in asking Question.
Thank you.
Try my php code:
From php.net datetime.format:
W: ISO-8601 week number of year, weeks starting on Monday.
The first calendar week of a year is that one which includes the first Thursday of that year.
So I have to rest one day to the start week date.
I assumed that the weeks correspond to the current year.
Input string:
$weeksString = "2W4,1W2,3W3,1W1,2W4";
Code:
<?php
$weeksArray = explode(",", $weeksString);
$result = array();
foreach($weeksArray as $visitsWeek) {
list($visits, $week) = explode("W", $visitsWeek);
$startDate = date("Y-m-d", strtotime(date("Y") . "W" . str_pad($week, 2, "0", STR_PAD_LEFT) . " -1 days"));
$endDate = date("Y-m-d", strtotime($startDate . " +6 days"));
$result[] = array("week" => $week, "startDate" => $startDate, "endDate" => $endDate);
}
?>
Output array $result:
array ( 0 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), 1 => array ( 'week' => '2', 'startDate' => '2021-01-10', 'endDate' => '2021-01-16', ), 2 => array ( 'week' => '3', 'startDate' => '2021-01-17', 'endDate' => '2021-01-23', ), 3 => array ( 'week' => '1', 'startDate' => '2021-01-03', 'endDate' => '2021-01-09', ), 4 => array ( 'week' => '4', 'startDate' => '2021-01-24', 'endDate' => '2021-01-30', ), )

php - show the first 3 items from an multi-dim array based on dates

so here is what I'm trying to do. I have structured a list of locations and dates in an multi-dim array. and I want to show the next 3 upcoming dates in the output based on date and location. Also I need to show only one date per location at any given moment so basically the closest date from location 1, 2 and 3 and so on, and every time one of them is passed I want to replace it with the next closest one. I might need to add more location later on but I still need to show the first 3 upcoming dates on the output. Should I structure my array differently?
Any help would be appreciated
Thanks
$event_dates = array(
'location-1' => array(
'date1' => array(
'start' => date('m-d-Y',strtotime('2017-7-30')),
'end' => date('m-d-Y',strtotime('2017-8-3')),
),
'date2' => array(
'start' => date('m-d-Y',strtotime('2018-2-18')),
'end' => date('m-d-Y',strtotime('2018-2-23')),
),
'date3' => array(
'start' => date('m-d-Y',strtotime('2018-7-29')),
'end' => date('m-d-Y',strtotime('2018-8-2')),
),
'date4' => array(
'start' => date('m-d-Y',strtotime('2019-1-27')),
'end' => date('m-d-Y',strtotime('2019-1-31')),
),
'date5' => array(
'start' => date('m-d-Y',strtotime('2019-7-28')),
'end' => date('m-d-Y',strtotime('2019-8-12')),
),
'date6' => array(
'start' => date('m-d-Y',strtotime('2020-1-16')),
'end' => date('m-d-Y',strtotime('2020-1-22')),
)
),
'location-2' => array(
'date1' => array(
'start' => date('m-d-Y',strtotime('2017-7-30')),
'end' => date('m-d-Y',strtotime('2017-8-13')),
),
'date2' => array(
'start' => date('m-d-Y',strtotime('2018-1-8')),
'end' => date('m-d-Y',strtotime('2018-2-11')),
),
'date3' => array(
'start' => date('m-d-Y',strtotime('2018-7-23')),
'end' => date('m-d-Y',strtotime('2018-8-12')),
),
'date4' => array(
'start' => date('m-d-Y',strtotime('2019-1-17')),
'end' => date('m-d-Y',strtotime('2019-1-23')),
),
'date5' => array(
'start' => date('m-d-Y',strtotime('2020-6-16')),
'end' => date('m-d-Y',strtotime('2020-6-22')),
)
),
'location-3' => array(
'date1' => array(
'start' => date('m-d-Y',strtotime('2017-5-12')),
'end' => date('m-d-Y',strtotime('2017-5-19')),
),
'date2' => array(
'start' => date('m-d-Y',strtotime('2018-9-22')),
'end' => date('m-d-Y',strtotime('2018-9-28')),
),
'date3' => array(
'start' => date('m-d-Y',strtotime('2018-3-12')),
'end' => date('m-d-Y',strtotime('2018-3-20')),
),
'date4' => array(
'start' => date('m-d-Y',strtotime('2019-12-9')),
'end' => date('m-d-Y',strtotime('2019-12-15')),
),
'date5' => array(
'start' => date('m-d-Y',strtotime('2020-11-16')),
'end' => date('m-d-Y',strtotime('2020-11-20')),
)
));
It's DateTime to the rescue!
Code (Demo):
$TZ=new DateTimeZone('UTC'); // make some sort of declaration on timezone
$today=new DateTime('NOW',$TZ);
$today->setTime(0,0,0);
foreach($event_dates as $locname=>$datearray){
foreach($datearray as $eventname=>$eventarray){ // I recommend changing 'date{n}' to an actual eventname in the array
$end=DateTime::createFromFormat('m-d-Y',$eventarray['end'],$TZ)->setTime(0,0,0);
if($today<=$end){
echo "$locname's $eventname";
$start=DateTime::createFromFormat('m-d-Y',$eventarray['start'],$TZ)->setTime(0,0,0);
$days=abs($today->diff($start)->days);
if($today==$start){
echo " begins today!";
}elseif($today==$end){
echo " concludes today!";
}elseif($today<$start){
echo " starts in $days day",($days!=1?"s":"");
}else{
echo " is in progress for $days day",($days!=1?"s":"");
}
echo "\n";
break;
}
}
}
Output:
location-1's date1 starts in 114 days
location-2's date1 starts in 114 days
location-3's date1 starts in 35 days
In an attempt to offer the most robust code, I've elected to turn my method into a function that will allow the designation of how many event dates to display. Also, I discovered that location-3 had one date out of order, so I've included a sort() to correct this.
Code: (Demo)
// for this sample array, $count=6 is the same as $count=-1
function currentAndUpcomingEvents($array,$location,$count=-1){ // default to -1 which will show all
$result=[];
$x=0;
$TZ=new DateTimeZone('UTC'); // make some sort of declaration on timezone
$today=new DateTime('NOW',$TZ);
$today->setTime(0,0,0);
// test a specific date: $today=DateTime::createFromFormat('m-d-Y','08-13-2017',$TZ)->setTime(0,0,0);
foreach($array[$location] as $eventname=>$daterange){
if($x==$count){break;}
$end=DateTime::createFromFormat('m-d-Y',$daterange['end'],$TZ)->setTime(0,0,0);
if($today<=$end){
$start=DateTime::createFromFormat('m-d-Y',$daterange['start'],$TZ)->setTime(0,0,0);
$days=abs($today->diff($start)->days);
$result[$days]="$location's $eventname"; // using $days as keys will allow sorting
if($today==$start){
$result[$days].=" begins today!";
}elseif($today==$end){
$result[$days].=" concludes today!";
}elseif($today<$start){
$result[$days].=" starts in $days day".($days!=1?"s":"");
}else{
$result[$days].=" is in progress for $days day".($days!=1?"s":"");
}
}
++$x;
}
ksort($result); // location-3's date3 was originally out of order
return $result;
}
foreach(array_keys($event_dates) as $location){
echo implode("\n",currentAndUpcomingEvents($event_dates,$location,6)); // whole array, level1 key, events per location count
echo "\n\n";
}
Output:
location-1's date1 starts in 114 days
location-1's date2 starts in 317 days
location-1's date3 starts in 478 days
location-1's date4 starts in 660 days
location-1's date5 starts in 842 days
location-1's date6 starts in 1014 days
location-2's date1 starts in 114 days
location-2's date2 starts in 276 days
location-2's date3 starts in 472 days
location-2's date4 starts in 650 days
location-2's date5 starts in 1166 days
location-3's date1 starts in 35 days
location-3's date3 starts in 339 days
location-3's date2 starts in 533 days
location-3's date4 starts in 976 days
location-3's date5 starts in 1319 days

Date and time stored on variables don't refresh while page

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

How to display PHP date in Turkish?

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");
?>

codeigniter - insert return true, without modifying database

I got this update script in a for :
if(isset($_POST[$i+1 . '_create_metier'])){
$new_entry = array(
'id_parent' => $current[0]->id,
'work' => $_POST[$i+1 . '_create_metier'],
'days' => $_POST[$i+1 . '_create_daycount'],
'price' => $_POST[$i+1 . '_create_price'],
'cr_date' => date('d-m-Y H:i:s'),
'user' => ''
);
var_dump($new_entry);
if(!$this->db->insert('meta_parent',$new_entry)){
echo 'fail';
$p3 = 0;
}else{
echo 'success';
}
}
The var_dump return a well filled array for my db structure, and 'success' is printed, implying the query worked.
But i don't get any modification in my DB. It's not my first use of codeigniter and i never had such an issue.
Thanks for help
I think the error is in the date format, MySQL accept the date in this format: Y-m-d H:i:s
Try replacing
'cr_date' => date('d-m-Y H:i:s'),
with
'cr_date' => date('Y-m-d H:i:s'),

Categories